简体   繁体   English

如何添加以防止正则表达式解析字符串的某些部分?

[英]How do I add prevent my regex from parsing certain parts of a string?

As per this answer , I am making use of a replaceAll() function to swap arbitrary strings in my javascript (Node) application: 按照这个答案 ,我正在使用replaceAll()函数在我的javascript(节点)应用程序中交换任意字符串:

this.replaceAll = function(str1, str2, ignoreCase)
{
    return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignoreCase?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
}

I would like to expand this regex, such that it does not try to match anything inside a set of <span>...</span> tags. 我想扩大这个表达式,这样它不会试图一组匹配任何<span>...</span>标记。 (I need to add HTML Span tags around parts of some strings, and I don't want to wrap anything in a span twice , if the patterns would duplicate [Ie, 'Foo' and 'Foobar' in the string "Foobarbaz"]) (我需要添加周围的一些字符串的部分HTML跨度标签,而我不想来包装一个跨度东西两次 ,如果模式将[字符串“Foobarbaz”在IE中,“富”和“Foobar的”]复制)

I am running multiple regex search/replaces on a single string, and I want to make sure that nothing gets processed multiple times. 我正在单个字符串上运行多个正则表达式搜索/替换,并且我想确保没有任何东西被处理多次。

My understanding is that I'll need a [<SPAN>] ... [</SPAN>] somehow, but I'm not quite sure on the specifics. 我的理解是,无论如何我都需要[<SPAN>] ... [</SPAN>] ,但是我不确定具体细节。 Any advice? 有什么建议吗?

I don't understand what your regexp does, but in general the technique is like this: 我不了解您的正则表达式会做什么,但总的来说,这种技术是这样的:

html = "replace this and this but not <span> this one </span> or <b>this</b> but this is fine"

// remove parts you don't want to be touched and place them in a buffer
tags = []
html = html.replace(/<(\w+).+?<\/\1>/g, function($0) {
    tags.push($0)
    return '@@' + (tags.length - 1)
})

// do the actual replacement
html = html.replace(/this/g, "that")

// put preserved parts back
html = html.replace(/@@(\d+)/g, function($0, $1) {
    return tags[$1]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何排除某些可变部分? - How do I exclude certain variable parts? 如何创建一个 Tampermonkey 脚本来防止页面的某些部分成为链接? - How can I create a Tampermonkey script that will prevent certain parts of a page from being links? 如何使用javascript选择字符串的一部分,向其中添加html标记或删除部分? - How do I select parts of a string with javascript, add html tag to that or delete parts? 如何在Regex的字符串末尾防止多余的字符? - How can I prevent extra characters at the end of my string in Regex? 我如何防止 formik yup ( regex ) 验证消失 - How do i prevent formik yup ( regex ) validation from disappearing 如何防止我的函数修改常量中的字符串? - How do I prevent my function from modifying a string within a constant? 我想在我用 three.js 安装的 obj 扩展 model 的某些部分添加一个热点。 我该怎么做? - I want to add a hotspot to certain parts of the obj extension model I have installed with three.js. how can I do it? 如何使我的HTML的某些部分无法选择? - How can I make certain parts of my html not selectable? 如何防止子字符串以父字符串结尾? - How do I prevent a child string from ending the parent string? 如何使用require导入模块的某些部分? - How do I import certain parts of a module with require?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM