1. <tfoot id='xUK6N'></tfoot>
      <legend id='xUK6N'><style id='xUK6N'><dir id='xUK6N'><q id='xUK6N'></q></dir></style></legend>
      • <bdo id='xUK6N'></bdo><ul id='xUK6N'></ul>

    2. <i id='xUK6N'><tr id='xUK6N'><dt id='xUK6N'><q id='xUK6N'><span id='xUK6N'><b id='xUK6N'><form id='xUK6N'><ins id='xUK6N'></ins><ul id='xUK6N'></ul><sub id='xUK6N'></sub></form><legend id='xUK6N'></legend><bdo id='xUK6N'><pre id='xUK6N'><center id='xUK6N'></center></pre></bdo></b><th id='xUK6N'></th></span></q></dt></tr></i><div id='xUK6N'><tfoot id='xUK6N'></tfoot><dl id='xUK6N'><fieldset id='xUK6N'></fieldset></dl></div>

        <small id='xUK6N'></small><noframes id='xUK6N'>

        Javascript RegExp 用于精确匹配具有特殊字符的多个单词

        Javascript RegExp for exact multiple words with special characters match(Javascript RegExp 用于精确匹配具有特殊字符的多个单词)
      1. <i id='sTGko'><tr id='sTGko'><dt id='sTGko'><q id='sTGko'><span id='sTGko'><b id='sTGko'><form id='sTGko'><ins id='sTGko'></ins><ul id='sTGko'></ul><sub id='sTGko'></sub></form><legend id='sTGko'></legend><bdo id='sTGko'><pre id='sTGko'><center id='sTGko'></center></pre></bdo></b><th id='sTGko'></th></span></q></dt></tr></i><div id='sTGko'><tfoot id='sTGko'></tfoot><dl id='sTGko'><fieldset id='sTGko'></fieldset></dl></div>
                <legend id='sTGko'><style id='sTGko'><dir id='sTGko'><q id='sTGko'></q></dir></style></legend>
                  <bdo id='sTGko'></bdo><ul id='sTGko'></ul>
                  <tfoot id='sTGko'></tfoot>

                  <small id='sTGko'></small><noframes id='sTGko'>

                    <tbody id='sTGko'></tbody>
                  本文介绍了Javascript RegExp 用于精确匹配具有特殊字符的多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 RegExp 进行多个单词匹配.它具有动态值,因此当出现像("这样的特殊字符时,它会将其作为表达式并显示 Uncaught SyntaxError: Invalid regular expression error.

                  I'm using RegExp for multiple words match. It has dynamic values so when a special character like "(" comes it takes that as an expression and shows Uncaught SyntaxError: Invalid regular expression error.

                  let text = 'working text and (not working text'
                  let findTerm = ['working text', '(not working text']
                  let replaceFromRegExp = new RegExp('\b'+`(${findTerm.join("|")})`+'\b', 'g')
                  text = text.replace(replaceFromRegExp, match => "<mark>" + match + "</mark>")
                  console.log(text)

                  推荐答案

                  A  单词边界 匹配以下三个位置中的任何一个:

                  A  word boundary matches any of the following three positions:

                  1. 在字符串的第一个字符之前,如果第一个字符是单词字符.
                  2. 在字符串的最后一个字符之后,如果最后一个字符是单词字符.
                  3. 字符串中两个字符之间,一个是单词字符,另一个不是单词字符.您需要通用的单词边界,在搜索词之前需要一个非单词字符或字符串开头,在搜索字符串之后需要一个非单词字符或字符串结尾.

                  请注意,您还需要按长度降序对 findTerm 项进行排序,以避免术语重叠问题.

                  Note you need to also sort the findTerm items in the descending order by length to avoid overlapping term issues.

                  最后,不要忘记转义 findTerm 项以在正则表达式模式中使用.

                  Finally, do not forget to escape the findTerm items to be used in a regex pattern.

                  你可以使用

                  let text = 'working text and (not working text'
                  let findTerm = ['working text', '(not working text']
                  findTerm.sort((a, b) => b.length - a.length);
                  let replaceFromRegExp = new RegExp(String.raw`(?:B(?!w)|(?=w))(?:${findTerm.map(x => x.replace(/[-/\^$*+?.()|[]{}]/g, '\$&')).join("|")})(?:(?<=w)|(?<!w)B)`, 'g')
                  // If the boundaries for special chars should not be checked remove B:
                  // let replaceFromRegExp = new RegExp(String.raw`(?:(?!w)|(?=w))(?:${findTerm.map(x => x.replace(/[-/\^$*+?.()|[]{}]/g, '\$&')).join("|")})(?:(?<=w)|(?<!w))`, 'g')
                  console.log(replaceFromRegExp)
                  text = text.replace(replaceFromRegExp, "<mark>$&</mark>")
                  console.log(text)

                  注意 <mark>$&</mark>>>match =><标记>"+ match + "</mark>",因为 $& 是对字符串替换模式中整个匹配值的反向引用.

                  Note that "<mark>$&</mark>" is a shorter way of saying match => "<mark>" + match + "</mark>", as $& is a backreference to the whole match value in a string replacement pattern.

                  正则表达式是

                  /(?:B(?!w)|(?=w))(?:(not working text|working text)(?:(?<=w)|(?<!w)B)/g
                  

                  或者

                  /(?:(?!w)|(?=w))(?:(not working text|working text)(?:(?<=w)|(?<!w))/g
                  

                  请参阅 regex #1 演示和 regex #2 演示.详情:

                  • (?:B(?!w)|(?=w)) - 如果下一个字符不是单词字符,则为非单词边界,或者如果下一个字符是单词字符,则单词边界
                  • (?:(?!w)|(?=w)) - 要么下一个字符必须是非单词字符,要么必须立即没有单词字符在当前位置的左边,下一个必须是单词 char(如果词以特殊字符开头,则不需要边界)
                  • (?:(not working text|working text) - 与 findTerm 数组中设置的替代模式之一匹配的非捕获组
                  • (?:(?<=w)|(?<!w)B) - 如果前面的 char 是单词 char,则为单词边界,或者如果前面的 char 不是单词 char,则为非单词边界
                  • (?:(?<=w)|(?<!w)) - 如果前一个 char 是单词 char,则下一个不能是word char,或者前一个char不应该是word char(如果term以特殊char结尾,则不需要边界)
                  • (?:B(?!w)|(?=w)) - either a non-word boundary if the next char is not a word char, or a word boundary if the next char is a word char
                  • (?:(?!w)|(?=w)) - either the next char must be a non-word char, or there must be no word char immediately to the left of the current location, and the next one must be a word char (if the term starts with a special char, no boundary is required)
                  • (?:(not working text|working text) - a non-capturing group matching one of the alternative patterns set in the findTerm array
                  • (?:(?<=w)|(?<!w)B) - either a word boundary if the preceding char is a word char, or a non-word boundary if the preceding char is not a word char
                  • (?:(?<=w)|(?<!w)) - if the previous char is a word char, the next one must not be a word char, or the previous char should not be a word char (if the term ends with a special char, no boundary is required)

                  这篇关于Javascript RegExp 用于精确匹配具有特殊字符的多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                  <legend id='TjwY0'><style id='TjwY0'><dir id='TjwY0'><q id='TjwY0'></q></dir></style></legend>
                    <tbody id='TjwY0'></tbody>

                  <tfoot id='TjwY0'></tfoot>
                • <i id='TjwY0'><tr id='TjwY0'><dt id='TjwY0'><q id='TjwY0'><span id='TjwY0'><b id='TjwY0'><form id='TjwY0'><ins id='TjwY0'></ins><ul id='TjwY0'></ul><sub id='TjwY0'></sub></form><legend id='TjwY0'></legend><bdo id='TjwY0'><pre id='TjwY0'><center id='TjwY0'></center></pre></bdo></b><th id='TjwY0'></th></span></q></dt></tr></i><div id='TjwY0'><tfoot id='TjwY0'></tfoot><dl id='TjwY0'><fieldset id='TjwY0'></fieldset></dl></div>

                  <small id='TjwY0'></small><noframes id='TjwY0'>

                      <bdo id='TjwY0'></bdo><ul id='TjwY0'></ul>