[英]RegEx Exactly Match Beginning and End of a String with a Wildcard in between
I am searching strings with the following format: ABC-001 我正在搜索以下格式的字符串:ABC-001
I would to construct a RegEx search from a user text input field so that I can search: 我将从用户文本输入字段构造一个RegEx搜索,以便可以搜索:
ABC* and match ABC-001, ABC-002, and so on...
ABC*2 and match ABC-002 but NOT ABC-012
ABC*12 and match ABC-012
I know I will need do some string manipulation to go from user input to a regular expression, but as an example I construct a regex like so: 我知道我需要做一些字符串操作才能从用户输入转换为正则表达式,但是作为示例,我构造了一个正则表达式,如下所示:
^ABC.*2$
Unfortunately, this matches ABC-002
and ABC-012
. 不幸的是,这匹配
ABC-002
和ABC-012
。
You can do this replace
on user input: 您可以在用户输入上执行以下
replace
:
var re = new RegExp(input.replace(/\*/g, '-0*'), "gi");
This will convert ABC*2
to /ABC-0*2/gi
regex and this will match ABC-002
but won't match ABC-012
. 这会将
ABC*2
转换为/ABC-0*2/gi
regex,它将与ABC-002
匹配,但与ABC-012
不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.