简体   繁体   English

如何验证某个字符是否在 javascript 中的两个专用字符内?

[英]How to verify that a certain character is inside two dedicated characters in javascript?

Let's say that the character we need to target is $ , and we need to verify if it lies between two ' & ' .假设我们需要定位的字符是$ ,我们需要验证它是否位于两个' & '之间。 The code will look something like this :代码看起来像这样:

var str = " Verify '$' "
var str2 = " Let's see if ' The $ is inside ! ' "
var str3 = " I got $ "
console.log(verify(str)) //=> true
console.log(verify(str2)) //=> true
console.log(verify(str3)) //=> false

Javascript String.prototype.includes() , can be used to verify if $ exists in the string but I am not understanding how to verify if it is inside ' & ' ? Javascript String.prototype.includes()可用于验证$是否存在于字符串中,但我不明白如何验证它是否在' & ' 内

You could look for $ , wrapped by ' .您可以查找$ ,由'包裹。

 const check = string => /'.*\\$.*'/.test(string); console.log([ " Verify '$' ", " Let's see if ' The $ is inside ! ' ", " I got $ "].map(check) );

This can be done using Regular Expressions :这可以使用正则表达式来完成:

 function verify(input) { const regex = RegExp(/'.*?\\$.*?'/); return regex.test(input); } var str = " Verify '$' " var str2 = " Let's see if ' The $ is inside ! ' " var str3 = " I got $ " console.log(verify(str)) //=> true console.log(verify(str2)) //=> true console.log(verify(str3)) //=> false

/'.*?\\$.*?'/ pattern description : /'.*?\\$.*?'/模式描述:

' matches the character ' literally (case sensitive) '字面上匹配字符' (区分大小写)

.*? matches any character (except for line terminators)匹配任何字符(行终止符除外)

*? Quantifier — Matches between zero and unlimited times , as few times as possible, expanding as needed (lazy)量词 - 在零次和无限次之间匹配,尽可能少的次数,根据需要扩展(懒惰)

\\$ matches the character $ literally (case sensitive) \\$字面上匹配字符$ (区分大小写)

.*? matches any character (except for line terminators)匹配任何字符(行终止符除外)

*? Quantifier — Matches between zero and unlimited times , as few times as possible, expanding as needed (lazy)量词 - 在零次和无限次之间匹配,尽可能少的次数,根据需要扩展(懒惰)

' matches the character ' literally (case sensitive) '字面上匹配字符' (区分大小写)

Its pretty hard to check if the $ is indeed between two ' and not between a pair of ' a ' and ' b 'b .很难检查$是否确实在两个'之间'而不是在一对' a '' b 'b So I would recommend iterating through the string and toggling a boolean whenever you see a quote.因此,我建议在看到引号时遍历字符串并切换布尔值。 If you find a $ when the boolean is true check if there is a ' in the rest of the text;如果在布尔值为true时找到$ ,请检查文本的其余部分是否有'

 console.log(verify("'$'")) //=> true console.log(verify("' $ '")) //=> true console.log(verify("'a' $ '")) //=> false console.log(verify(" ' $ ")) //=> false function verify(text) { var openQuotes = false; for (var index = 0; index < text.length; index++) { if (text[index] === "'") openQuotes = !openQuotes; else if (text[index] === "$" && openQuotes && text.indexOf("'", index) !== -1) return true; } return false; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Javascript中隐藏某些字符 - How to hide certain characters in Javascript 如何在JavaScript中的字符串的最后两个字符之间替换一个字符? - How do I replace a character between the last occurrence of two characters in a string in JavaScript? 如何从 javascript 中的字符串中获取两个字符之间的最后一个字符? - How to get the last before character between two characters from string in javascript? 如何使用JavaScript突出显示两个已知字符之间的包含随机字符的子字符串? - How to highlight a substring containing a random character between two known characters using javascript? 如何判断一个字符串是否包含JavaScript中的某个字符? - How to tell if a string contains a certain character in JavaScript? 替换字符:Javascript 中如何识别网页中的字符? - Replacing characters: how is a character in a webpage indentified in Javascript? Javascript正则表达式将所有字符匹配到内容中的某些点 - Javascript regex to match all characters to certain point inside content 如何使用 javascript 或 jquery 验证自定义属性中是否存在某个值? - How to verify if a certain value is present in a custom attribute using javascript or jquery? 如何在javascript中删除字符串末尾的某个字符 - how to remove a certain character at the end of the string in javascript 如何通过Javascript检测某个字符之后是否有任何内容? - How to detect if there are anything after a certain character by Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM