JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
CSS
body{
font-family: '微軟正黑體';
font-size: 14px;
line-height: 1.8;
background-color: #fff;
}
JavaScript
//正規式
const reg1 = /虧雞/;
const reg2 = /虧G/;
const str = '今天是虧雞福來爹!!';
$('body').append(`<p>str = ${str} <br> reg1 = /虧雞/ <br> reg2 = /虧G/ </p>`);
//使用.test()檢查是否有符合字串
$('body').append(`<p>使用test(): str 有符合 reg1 嗎? ${reg1.test(str)} <p>`);
$('body').append(`<p>使用test(): str 有符合 reg2 嗎? ${reg2.test(str)} </p>`);
//使用.match()抓出符合的字串,有找到會回傳該字串,沒有回傳null
$('body').append(`<p>使用match(): str 符合 reg1 的字串為 ${str.match(reg1)} </p>`);
$('body').append(`<p>使用match(): str 符合 reg2 的字串為 ${str.match(reg2)} </p>`);
//使用.replace()抓出符合的字串,並替換成其他內容
$('body').append(`<p>使用replace(): ${str.replace(reg1, '敲虧G')} </p>`);
//使用.split()以正規式做切割
$('body').append(`<p>使用split(): ${str.split(reg1).join('')} </p>`);