Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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,我有: 现在我想将正则表达式更改为匹配除当前匹配项之外的所有项。我该怎么做 例如: 当前正则表达式: here is some text [anything123][/21something] and here is too [sometext][/afewtext] and here // ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^ 我想要这个: here

我有:

现在我想将正则表达式更改为匹配除当前匹配项之外的所有项。我该怎么做

例如:

当前正则表达式:

   here is some text [anything123][/21something] and here is too [sometext][/afewtext] and here

//                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^                 ^^^^^^^^^^^^^^^^^^^^^
我想要这个:

   here is some text [anything123][/21something] and here is too [sometext][/afewtext] and here
// ^^^^^^^^^^^^^^^^^^                           ^^^^^^^^^^^^^^^^^                     ^^^^^^^^^

匹配内部内容或捕获外部内容

演示:

var str='这里是一些文本[anything123][/21something],这里也是[sometext][/afewtext]和这里';
var regex=/\[.*?\]\;([^[]+)/g;
var-res='';
//这样做,直到有一个匹配
while(m=regex.exec(str)){
//如果第一个捕获的组出现
if(m[1]){
//将匹配项追加到结果字符串
res+=m[1];
}
}
控制台日志(res);

document.body.innerHTML=res;//仅用于演示
只需使用
replace
替换匹配项。
str.replace(/(\[.*?\])/g',)
@Tushar我想知道除了正则表达式中的模式之外,还有什么方法可以匹配所有的东西吗?我会按照Tushar的建议使用replace,这就是我要说的..参见
text.split(/\[.\]]/g)
@stack这也有同样的作用。你只需要获得第一个捕获的组并加入他们。@Tushar是的,我知道
$1
的内容正是我需要的。顺便说一句,我可以完全按照你说的那样做(使用
.replace()
),但老实说,我想知道如何使用lookahead
(?!)
?(因为我在某处看到了一种使用前瞻的方法)@Tushar谢谢你……是的,它是有效的……但我想我不能告诉你我到底想要什么。
:-)
!!我想知道的是:如何定义一个正则表达式来匹配除其模式(纯正则表达式)之外的所有东西@堆栈,但这在JS正则表达式中不起作用:p@stack因为那是。但这是昂贵的,更多的小虾会失败。
   here is some text [anything123][/21something] and here is too [sometext][/afewtext] and here
// ^^^^^^^^^^^^^^^^^^                           ^^^^^^^^^^^^^^^^^                     ^^^^^^^^^
\[.*?\]|([^[]+)