正则表达式匹配除括号中的所有单词 – javascript
发布时间:2020-12-14 05:50:17 所属栏目:百科 来源:网络整理
导读:我正在使用以下正则表达式来匹配所有单词: mystr.replace(/([^W_]+[^s-]*) */g,function (match,p1,index,title) {...} 请注意,单词可以包含特殊字符,如德语元音. 如何匹配除括号内的所有单词? 如果我有以下字符串: here w?re c'è (don't match this o
我正在使用以下正则表达式来匹配所有单词:
mystr.replace(/([^W_]+[^s-]*) */g,function (match,p1,index,title) {...} 请注意,单词可以包含特殊字符,如德语元音. 如果我有以下字符串: here w?re c'è (don't match this one) match this 我想得到以下输出: here w?re c'è match this 尾随空格并不重要. 编辑: Here W?re C'è (don't match this one) Match This 解决方法
试试这个:
var str = "here w?re c'è (don't match this one) match this"; str.replace(/([^)]*)/g,'') // remove text inside parens (& parens) .match(/(S+)/g); // match remaining text // ["here","w?re","c'è","match","this"] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |