在早期帖子中未涵盖的情况下否定 Javascript 中的正则表达式
Negating a regular expression in Javascript in situations not covered in earlier posts
在您将其标记为重复之前,让我向您保证,我已经完成了,希望是,在 Whosebug 上针对该主题提出的所有问题,包括以下内容:
How can I invert a regular expression in JavaScript?
negation in regular expression
我正在尝试使用如下语法解析字符串:
contents = "Some Random Text1 <@@Matched text 1@@> Some Random Text2 <@@Matched text 2@@> Some Random Text3";
我用的是普通快递:
reg_exp = /\<\!\!.*?\!\!\>/g
然后使用以下代码提取匹配文本并从中提取一个长字符串,每个匹配之间有一个空白分隔符。
while ((matched_array = reg_exp.exec(contents)) != null) {
matched_text += (matched_array[0] + "");
};
这个returnsmatched_text为:
<@@Matched text 1@@> <@@Matched text 2@@>
一切正常。
现在我想获取匹配文本之外的所有文本的子字符串。
我无法设置正则表达式来对匹配文本之外的文本执行相同的操作,从而为上述示例生成以下结果字符串:
Some Random Text1 Some Random Text2 Some Random Text3
我尝试了上面引用的帖子中提供的所有可能的解决方案,包括 (?! or ^ after grouping () 等
使用string.replace
函数将那些匹配的字符替换为空字符串。
var string = "Some Random Text1 <@@Matched text 1@@> Some Random Text2 <@@Matched text 2@@> Some Random Text3";
alert(string.replace(/\s*<@@.*?@@>/g, ''))
在您将其标记为重复之前,让我向您保证,我已经完成了,希望是,在 Whosebug 上针对该主题提出的所有问题,包括以下内容:
How can I invert a regular expression in JavaScript?
negation in regular expression
我正在尝试使用如下语法解析字符串:
contents = "Some Random Text1 <@@Matched text 1@@> Some Random Text2 <@@Matched text 2@@> Some Random Text3";
我用的是普通快递:
reg_exp = /\<\!\!.*?\!\!\>/g
然后使用以下代码提取匹配文本并从中提取一个长字符串,每个匹配之间有一个空白分隔符。
while ((matched_array = reg_exp.exec(contents)) != null) {
matched_text += (matched_array[0] + "");
};
这个returnsmatched_text为:
<@@Matched text 1@@> <@@Matched text 2@@>
一切正常。 现在我想获取匹配文本之外的所有文本的子字符串。
我无法设置正则表达式来对匹配文本之外的文本执行相同的操作,从而为上述示例生成以下结果字符串:
Some Random Text1 Some Random Text2 Some Random Text3
我尝试了上面引用的帖子中提供的所有可能的解决方案,包括 (?! or ^ after grouping () 等
使用string.replace
函数将那些匹配的字符替换为空字符串。
var string = "Some Random Text1 <@@Matched text 1@@> Some Random Text2 <@@Matched text 2@@> Some Random Text3";
alert(string.replace(/\s*<@@.*?@@>/g, ''))