第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

正則表達(dá)式匹配星號、波浪號、破折號和方括號

正則表達(dá)式匹配星號、波浪號、破折號和方括號

不負(fù)相思意 2021-10-14 16:50:21
我一直在嘗試編寫一個(gè)正則表達(dá)式來匹配星號、波浪號、破折號和方括號。我擁有的:const str = "The] quick [brown] fox **jumps** over ~~the~~ lazy dog --- in the [woods";console.log(str.match(/[^\]][^\[\]]*\]?|\]/g));// [//     "The]",//     " quick ",//     "[brown]",//     " fox **jumps** over ~~the~~ lazy dog --- in the ",//     "[woods"// ];我想要的是:[    "The]",    " quick ",    "[brown]",    " fox ",    "**jumps**",    " over ",    "~~the~~",    " lazy dog ",    "---",    " in the ",    "[woods"];編輯:字符串的更多示例/組合是:"The] quick brown fox jumps [over] the lazy [dog"// [ "The]", " quick brown fox jumps ", "[over]", " the lazy ", "[dog" ]"The~~ quick brown fox jumps [over] the lazy **dog"// [ "The~~", " quick brown fox jumps ", "[over]", " the lazy ", "**dog" ]編輯2:我知道這很瘋狂,但是:"The special~~ quick brown fox jumps [over the] lazy **dog on** a **Sunday night."// [ "The special~~", " quick brown fox jumps ", "[over the]", " lazy ", "**dog on**", " a ", "**Sunday night" ]
查看完整描述

2 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以使用此正則表達(dá)式進(jìn)行更多更改以包含您想要的匹配項(xiàng):


const re = /\[[^\[\]\n]*\]|\b\w+\]|\[\w+|\*\*.+?(?:\*\*|$)|-+|(?:^|~~).+?~~|[\w ]+/mg;

const arr = [

'The special~~ quick brown fox jumps [over the] lazy **dog on** a **Sunday night.',

'The] quick brown fox jumps [over] the lazy [dog',

'The] quick [brown] fox **jumps** over ~~the~~ lazy dog --- in the [woods'

];


var n;

arr.forEach( str => {

  m = str.match(re);

  console.log(m);

});


查看完整回答
反對 回復(fù) 2021-10-14
?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以使用此正則表達(dá)式來拆分字符串。它在分隔符(**,~~或[])之一與匹配的分隔符或字符串的開始/結(jié)束之間拆分文本上的字符串;或在一系列連字符 ( -) 上。它使用捕獲組來確保正則表達(dá)式匹配的字符串出現(xiàn)在輸出數(shù)組中:


((?:\*\*|^)[A-Za-z. ]+(?:\*\*|$)|(?:~~|^)[A-Za-z. ]+(?:~~|$)|(?:\[|^)[A-Za-z. ]+(?:\]|$)|-+

const re = /((?:\*\*|^)[A-Za-z. ]+(?:\*\*|$)|(?:~~|^)[A-Za-z. ]+(?:~~|$)|(?:\[|^)[A-Za-z. ]+(?:\]|$)|-+)/;

const str = [

  'The] quick [brown] fox **jumps** over ~~the~~ lazy dog --- in the [woods',

  'The] quick brown fox jumps [over] the lazy [dog',

  'The~~ quick brown fox jumps [over] the lazy **dog',

  'The special~~ quick brown fox jumps [over the] lazy **dog on** a **Sunday night.'];


str.forEach(v => console.log(v.split(re).filter(Boolean)));

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復(fù) 2021-10-14
  • 2 回答
  • 0 關(guān)注
  • 928 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號