[英]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@_abcdef
或test__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
This regex has 3 assertions before matching abc
: 此正则表达式在匹配
abc
之前有3个断言:
(?<![a-z0-9])
: Fail the match when previous character is not alphanumeric (?<![a-z0-9])
:前一个字符不是字母数字时,匹配失败 (?<!\\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])
:如果我们有一个非字母数字字符而没有一些非空格字符,则匹配失败。 (?<!^[^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.