检查字符串中的重复值
Check duplicate value in string
我有包含值的字符串,例如我想检查特定值是否存在不止一次
"aaaa:123&bbbbb ccccc*fooo barr/bazzzz 123"
字符串可以包含 sigh,例如 : and & and /
有一种方法可以知道我是否在这个字符串中有两次数字 123?
我正在使用下划线,但如果需要也可以使用 lodash。
也许您可以手动遍历它以提高效率,但这很有效并且非常简单:
console.log(str.match(/123/g).length > 1);
要使用变量,您需要使用正则表达式:
var str = "aaaa:123&bbbbb ccccc*fooo barr/bazzzz 123";
var v = "2";
var regex = new RegExp("1"+v+"3", "g");
var matches = str.match(regex);
console.log(matches != null && matches.length > 1) // matches will be null if there are no finds
我有包含值的字符串,例如我想检查特定值是否存在不止一次
"aaaa:123&bbbbb ccccc*fooo barr/bazzzz 123"
字符串可以包含 sigh,例如 : and & and /
有一种方法可以知道我是否在这个字符串中有两次数字 123?
我正在使用下划线,但如果需要也可以使用 lodash。
也许您可以手动遍历它以提高效率,但这很有效并且非常简单:
console.log(str.match(/123/g).length > 1);
要使用变量,您需要使用正则表达式:
var str = "aaaa:123&bbbbb ccccc*fooo barr/bazzzz 123";
var v = "2";
var regex = new RegExp("1"+v+"3", "g");
var matches = str.match(regex);
console.log(matches != null && matches.length > 1) // matches will be null if there are no finds