[英]Regex to match all words but the one beginning and ending with special chars
我正在為Java正則表達式苦苦掙扎。
這是一個我想從中匹配所有單詞的字符串,除了以\\ + \\ s為前綴並且以\\ s \\ +為后綴的單詞:
這是我+ cr腳+的例子
正則表達式應匹配:
這是 我 + cr腳+的例子
匹配1:這是
比賽2:我的
匹配3:示例
您可以在上下文中使用替換運算符,將要排除的內容放在左側( 也就是說,將其扔掉,這是垃圾 ),然后將要匹配的內容放在捕獲組的右側。
\+[^+]+\+|([\w-]+)
范例 :
var re = /\+[^+]+\+|([\w-]+)/g,
s = "this-is my + crappy + example",
match,
results = [];
while (match = re.exec(s)) {
results.push(match[1]);
}
console.log(results.filter(Boolean)) //=> [ 'this-is', 'my', 'example' ]
或者,您可以在+
字符之間替換,然后匹配您的單詞。
var s = 'this-is my + crappy + example',
r = s.replace(/\+[^+]*\+/g, '').match(/[\w-]+/g)
console.log(r) //=> [ 'this-is', 'my', 'example' ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.