正则表达式模式,匹配该单词中任何位置包含至少一个星号的所有单词。
str = "t*e *pp locall* so my q**st*on is **stinct *ro* t*eir's. *ev***heles* lol ****"
应该匹配
t*e
*pp
locall*
所以我的q**st*on
是**stinct
*ro*
t*eir's
。 *ev***heles*
lol ****
[\w-']*(?:\*+[\w-']*)+
[\w-']*
任何单词匹配任何单词character,-
或'
(?:\*+[\w-']*)+
匹配以下一次或多次
\*+
匹配*
一次或多次
[\w-']*
任何单词匹配任何单词character,-
或'
let s = `t*e *pp locall* so my q**st*on is **stinct *ro* t*eir's. *ev***heles* lol ****`
let r = /[\w-']*(?:\*+[\w-']*)+/g
while(m = r.exec(s)) {
console.log(m[0])
}
也许你在寻找这个?
/[^\s\.,?!]*\*+[^\s\.,?!]*/g
[^\s\.,?!]*
匹配任何字符零次或多次不是空格或标点符号。\*+
匹配*
一次或多次。[^\s\.,?!]*
继续匹配字符,直到遇到空格或标点符号,并终止匹配。我投入了更多标点符号,因为你似乎想要从匹配中省略它:
var str = "t*e *pp locall* so my q**st*on is **stinct *ro* t*eir's. *ev***heles* lol ****"
console.log(str.match(/[^\s\.,?!]*\*[^\s\.,?!]*/g))