Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式匹配除括号中的单词以外的所有单词-javascript_Javascript_Regex - Fatal编程技术网

正则表达式匹配除括号中的单词以外的所有单词-javascript

正则表达式匹配除括号中的单词以外的所有单词-javascript,javascript,regex,Javascript,Regex,我使用以下正则表达式匹配所有单词: mystr.replace(/([^\W_]+[^\s-]*) */g, function (match, p1, index, title) {...} 注意,单词可以包含特殊字符,如德语Umlauts。 如何匹配除括号内的单词以外的所有单词 如果我有以下字符串: here wäre c'è (don't match this one) match this 我希望获得以下输出: here wäre c'è match this 尾随空格并不重要。 有

我使用以下正则表达式匹配所有单词:

mystr.replace(/([^\W_]+[^\s-]*) */g, function (match, p1, index, title) {...}
注意,单词可以包含特殊字符,如德语Umlauts。 如何匹配除括号内的单词以外的所有单词

如果我有以下字符串:

here wäre c'è (don't match this one) match this
我希望获得以下输出:

here
wäre
c'è
match
this
尾随空格并不重要。 有没有一种简单的方法可以通过javascript中的正则表达式实现这一点

编辑: 我无法删除括号中的文本,因为最后一个字符串“mystr”也应该包含此文本,而字符串操作将在匹配的文本上执行。“mystr”中包含的最后一个字符串可能如下所示:

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"]

Thomas,重新提出这个问题,因为它有一个简单的解决方案,没有提到,不需要替换然后匹配(一步而不是两步)。(在对有关的一般问题进行研究时发现了您的问题。)

下面是我们的简单正则表达式(请在工作时查看,查看右下面板中的组捕获):

交替的左侧匹配完整的
(括号中的短语)
。我们将忽略这些匹配。右侧匹配并捕获组1中的单词,我们知道它们是正确的单词,因为它们与左侧的表达式不匹配

此程序显示如何使用正则表达式(请参阅中的匹配项):


var subject='here wäre c\'è(不匹配此项)匹配此项';
var regex=/\(.*?\)|([^\W\u]+[^\ s-]*)/g;
var group1Caps=[];
var match=regex.exec(主题);
//将组1捕获放入数组中
while(匹配!=null){
如果(匹配[1]!=null)group1Caps.push(匹配[1]);
match=regex.exec(主题);
}
文档。写入(“
***匹配***
”; 如果(group1Caps.length>0){ 对于(输入group1Caps)文档。写入(group1Caps[key],“
”; }
参考


我认为使用单个正则表达式是不可能的,可能您需要先将括号与其内容切掉。您需要考虑嵌套的(比如这个(甚至这个))括号吗?如果是这样,您将不得不对嵌套设置一个上限,或者使用非基于RE的解决方案。无需考虑嵌套括号。可以有多个parentese,但它们不会嵌套。e、 “我接受法布里齐奥的回答,因为它是正确的,然后才把我的问题说得更具体。为了解决我的问题,我将在回调函数中搜索开始和结束参数。这不如正则表达式好,但它工作得很好。顺便说一句,paren在字符类中没有意义,因此它们不需要转义-
[^]
很好。任何其他元字符也是如此。是的,的确如此。我总是逃避特殊角色作为个人习惯,即使没有必要。谢谢法布里齐奥,但我的问题不够具体。我无法删除括号中的字符串,因为应返回整个字符串(包括括号中的文本),同时将对匹配项执行字符串操作。由于最终输出包含修改的匹配项和括号中被忽略的文本,因此在将修改后的副本与原始副本相结合时,我会遇到问题。请帮我做这个
\(.*?\)|([^\W_]+[^\s-]*)
<script>
var subject = 'here wäre c\'è (don\'t match this one) match this';
var regex = /\(.*?\)|([^\W_]+[^\s-]*)/g;
var group1Caps = [];
var match = regex.exec(subject);

// put Group 1 captures in an array
while (match != null) {
    if( match[1] != null ) group1Caps.push(match[1]);
    match = regex.exec(subject);
}

document.write("<br>*** Matches ***<br>");
if (group1Caps.length > 0) {
   for (key in group1Caps) document.write(group1Caps[key],"<br>");
   }

</script>