javascript - 如何检查 javascript 输入中的特殊字符 "<"或 ">"?

标签 javascript regex

尝试在 javascript 的用户输入中检查 < 或 >。 知道正则表达式是什么吗? 这个正则表达式似乎不起作用

 var spclChar=/^[<>]$/;
        if(searchCriteria.firstName.match(spclChar)){
            return true;
        }else {
            return false;
        }

最佳答案

您可以使用这样的字符类:

[<>]
or
[><]

Working demo

顺便说一句,您的问题中有有用的评论,例如 Sam

/[<>]/

https://regex101.com/r/oP0nG0/1

Marc B

your regex is searching for a < at the START of the string, or a > at the END of the string. if you want them ANYWHERE in the string, then dump the ^ and $. – Marc B 7 mins ago

关于javascript - 如何检查 javascript 输入中的特殊字符 "<"或 ">"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190052/

相关文章:

javascript - Vanilla JavaScript,与属于类组的单个 DOM 元素交互,而不影响组中的其他元素

javascript - TypeError : google. visualization.DataTable 不是构造函数

javascript - 在 Firefox 中关闭窗口

java - 在开发网络客户端应用程序时,你能推荐开发集成工具吗?

java - Java 中的正则表达式在替代匹配中使用相同的组

php - 尝试使用正则表达式查找字母组

c# - 如何使用 C# 修剪正则表达式中的空白

javascript - 麻烦 - Node js 将文件从文件夹 x 移动到文件夹 y

java - 如何在 cucumber 测试中匹配括号

来自相同正则表达式字符串的 C# 和 JavaScript 结果非常不同 - 如何使结果更一致?