Javascript Javascript通配符变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8920948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript wildcard variable?
提问by deceze
The value of product_id
might be some combination of letters and numbers, like: GB47NTQQ.
的值product_id
可能是字母和数字的某种组合,例如:GB47NTQQ。
I want to check to see if all but the 3rd and 4th characters are the same.
我想检查除第 3 个和第 4 个字符之外的所有字符是否相同。
Something like:
就像是:
if product_id = GBxxNTQQ //where x could be any number or letter.
//do things
else
//do other things
How can I accomplish this with JavaScript?
如何使用 JavaScript 完成此操作?
回答by Lawson Kurtz
Use regular expression and string.match(). Periods are single wildcard characters.
使用正则表达式和 string.match()。句点是单个通配符。
string.match(/GB..NTQQ/);
回答by deceze
回答by RobG
Answers so far have suggested match
, but test
is likely more appropriate as it returns trueor false, whereas match
returns nullor an array of matches so requires (implicit) type conversion of the result within the condition.
到目前为止的答案建议match
,但test
可能更合适,因为它返回true或false,而match
返回null或匹配数组,因此需要(隐式)条件内的结果类型转换。
if (/GB..NTQQ/.test(product_id)) {
...
}
回答by Adam Dymitruk
if (myString.match(/regex/)) { /*Success!*/ }
You can find more information here: http://www.regular-expressions.info/javascript.html
您可以在此处找到更多信息:http: //www.regular-expressions.info/javascript.html