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);
});

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; }
添加回答
舉報(bào)