简体   繁体   English

Javascript正则表达式,用于搜索具有多个特殊字符(非空格)的单词中的字符串

[英]Javascript regular expression to search a string in word having multiple special characters (non space)

Requirement is to find if the search string is present in the given string with below conditions. 要求是在以下条件下查找给定字符串中是否存在搜索字符串。

Condition 1 Search string should be found at the begin of the word ie, no special characters preceding it. 条件1搜索字符串应在单词的开头找到,即在单词之前没有特殊字符。

  • abc should match in string that begins with abc like abcdef any where in the sentence. abc应该匹配以abcdef开头的abc字符串,在句子中的任何位置。

  • abc should NOT match in xabcdef should NOT match as it is not starting with 'abc' xabcdef abc不匹配,因为它不是以'abc'开头

Condition 2 If the string is preceded with some special character, then it should also have some text before special characters. 条件2如果字符串以某些特殊字符开头,则在特殊字符之前还应该有一些文本。

  • abc should match in test_abcdef - as 'abc' is preceded with 'test_' abc应该在test_abcdef匹配-因为'abc'之前是'test_'

  • abc should NOT match in _abcdef - as it is starting with '_' without any text before _ abc_abcdef不匹配-因为它以“ _”开头,而_之前没有任何文本

Below regular expression is not finding abc if string has multiple special characters ex in string test@_abcdef or test__abcdef . 如果字符串在字符串test@_abcdeftest__abcdef具有多个特殊字符,则在正则表达式下面找不到abc

In the regular expression not sure how to add quantifier in ' (?<=[A-Za-z0-9][^A-Za-z0-9])abc ' where [^A-Za-z0-9] is checking for SINGLE non alpha numeric character. 在正则表达式中,不确定如何在' (?<= [A-Za-z0-9] [^ A-Za-z0-9])abc '中添加量词,其中[^ A-Za-z0-9]为检查单个非字母数字字符。

What is the syntax to add 0 or more special character in reqex (?<=... ) 在reqex (?<=... )添加0个或多个特殊字符的语法是什么

Regular Expression tried in Online Regex Tester 在线正则表达式测试器中 尝试过的正则 表达式

/^(?<![^A-Za-z0-9])abc|(?<=[A-Za-z0-9][^A-Za-z0-9])abc|(?<=\ )abc/g

Sample Text : 示范文本 :

abcdef abcdef _abcdef xabcdef test_abcdef test__abcdef abc

You can apply all the assertions without alternation here: 您可以在此处应用所有断言而无需更改:

/(?<![a-z0-9])(?<!^[^a-z0-9])(?<!\s[^a-z0-9])abc/igm

RegEx Demo 正则演示

This regex has 3 assertions before matching abc : 此正则表达式在匹配abc之前有3个断言:

  1. (?<![a-z0-9]) : Fail the match when previous character is not alphanumeric (?<![a-z0-9]) :前一个字符不是字母数字时,匹配失败
  2. (?<!\\s[^a-z0-9]) : Fail the match when we have a non-alphanumeric character without preceding with some non-space character. (?<!\\s[^a-z0-9]) :如果我们有一个非字母数字字符而没有一些非空格字符,则匹配失败。
  3. (?<!^[^a-z0-9]) : Fail the match when we have a non-alphanumeric character at line start (?<!^[^a-z0-9]) :当我们在行首有一个非字母数字字符时,匹配失败

Also note that lookbehind support in Javascript is still limited to new browsers only. 另请注意,Java语言中的向后支持仍然仅限于新的浏览器。

As regexes don't allow variable lenght lookbehind assertions, I don't think you can match just 'abc' but at the same time discard things like " _abc" " __abc" " ___abc", "____abc", etc. 由于正则表达式不允许变量长度位于断言之后,因此我认为您不能只匹配'abc',但同时丢弃诸如“ _abc”“ __abc”“ ___ abc”,“ ____ abc”之类的东西。

I would suggest to do it in 2 steps: 我建议分两个步骤进行操作:

First, try to match all required cases with a regexp without limiting yourself to just match 'abc' 首先,尝试使用正则表达式匹配所有必需的情况,而不会限制自己仅匹配'abc'

(?:(?!abc[^a-zA-Z0-9\s]+)[a-zA-Z0-9]+[^a-zA-Z0-9\s]+|^|\s)(abc)

https://regex101.com/r/bAo05D/3 https://regex101.com/r/bAo05D/3

Then, just recalculate abc index with: abc_index = whole_regexp_index + length(regexp_matched_string) - length(abc) 然后,只需使用以下abc_index = whole_regexp_index + length(regexp_matched_string) - length(abc)重新计算abc索引: abc_index = whole_regexp_index + length(regexp_matched_string) - length(abc)

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

相关问题 Javascript 具有特殊字符的正则表达式密码验证 - Javascript regular expression password validation having special characters JavaScript正则表达式替换字符串中的转义特殊字符 - JavaScript Regular Expression to Replace Escape Special Characters from String 正则表达式,包括单词搜索中的前导空格 - Regular expression including leading space in word search 不允许特殊字符的 Javascript 正则表达式 - Javascript Regular Expression that diallows special characters javascript-正则表达式字母数字和特殊字符 - javascript- regular expression alphanumeric and special characters 用于识别所有特殊字符的javascript正则表达式 - javascript regular expression to recognize all special characters 使用javascript删除特殊字符的正则表达式 - Regular expression using javascript for removing the special characters Javascript正则表达式:匹配除允许的特殊字符外的所有非单词 - Javascript regular expression: match any not-word except allowed special characters 整个单词匹配的Javascript正则表达式也可能包含特殊字符 - Javascript Regular Expression for whole word match which may contains special characters also JavaScript:用普通空格替换不间断空格和特殊空格字符 - JavaScript: replace non breaking space and special space characters with ordinary space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM