我想检查字符串是否以以下结尾
- v{number}
例如
hello world false
hello world - v2 true
hello world - v false
hello world - v88 true
不完全确定如何执行此正则表达式。
var str = 'hello world - v1';
var patt = new RegExp("/^ - v\d*$/");
var res = patt.test(str);
console.log(res);
我该如何修复上述正则表达式?
只需使用它,而无需在开始时检查其他任何内容
- v\d+$
这样你就可以确保它以- v
后跟至少一位数字。看到它住在https://regex101.com/r/pB6vP5/1
那么,你的表达方式需要是:
var patt = new RegExp("- v\\d+$");
As anubhava在另一个答案中说的:
var str = 'hello world - v15';
var patt = new RegExp("- v\\d+$");
var res = patt.test(str);
console.log(res);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)