需要在字符串中搜索@@@ JavaSCript
Need to search for @@@ in a string JavaSCript
var inputString = '$$';
// var inputString = '$';
inputString.indexOf('$') returns > -1 index when inputString is '$'.
inputString.indexOf('$$') returns > -1 index when inputString is '$' - this is not desired output.
当且仅当完全匹配“$$”在一个更大的字符串中而不是其中的一个字符时,我需要一些东西给出 true/returns 值找到了。
例如'test $$'为真
'test $' 为假
“$$ 测试”为真
“$$ 测试 $$”为真
我正在尝试查看 Javascript 中是否有执行此操作的内置方法。 'includes' 或 'search' 也有与 indexOf 相同的问题。有没有其他方法可以解决这个问题,也许使用效率不高的特定正则表达式?
我有点好奇你为什么有这些具体要求,但它可以很快创建。您的示例与您的描述不符,以下函数执行您描述的操作,"gives true/returns value if and only if there is an exact match '$$' within a larger string"。如果你想 false
当没有匹配时,只需相应地替换 return 或使用 RegExp.prototype.test /$$/.test("ug23487$§%fd$$s8w374") === true;
function contains(x) {
if (this.indexOf(x) !== -1) return true;
else return undefined;
}
String.prototype.contains = contains;
console.log(contains.call("$", "$$"));
console.log("$".contains("$$"));
console.log("ug23487$§%fd$$s8w374".contains("$$"));
请注意,您提到的几乎所有功能都可以在这里使用,而不是使用 indexOf
(略微调整 parameters/code)
var inputString = '$$';
// var inputString = '$';
inputString.indexOf('$') returns > -1 index when inputString is '$'.
inputString.indexOf('$$') returns > -1 index when inputString is '$' - this is not desired output.
当且仅当完全匹配“$$”在一个更大的字符串中而不是其中的一个字符时,我需要一些东西给出 true/returns 值找到了。
例如'test $$'为真 'test $' 为假 “$$ 测试”为真 “$$ 测试 $$”为真
我正在尝试查看 Javascript 中是否有执行此操作的内置方法。 'includes' 或 'search' 也有与 indexOf 相同的问题。有没有其他方法可以解决这个问题,也许使用效率不高的特定正则表达式?
我有点好奇你为什么有这些具体要求,但它可以很快创建。您的示例与您的描述不符,以下函数执行您描述的操作,"gives true/returns value if and only if there is an exact match '$$' within a larger string"。如果你想 false
当没有匹配时,只需相应地替换 return 或使用 RegExp.prototype.test /$$/.test("ug23487$§%fd$$s8w374") === true;
function contains(x) {
if (this.indexOf(x) !== -1) return true;
else return undefined;
}
String.prototype.contains = contains;
console.log(contains.call("$", "$$"));
console.log("$".contains("$$"));
console.log("ug23487$§%fd$$s8w374".contains("$$"));
请注意,您提到的几乎所有功能都可以在这里使用,而不是使用 indexOf
(略微调整 parameters/code)