[英]Javascript escapes String escape sequences 's backslash
I want to detect wheather user has entered any whitespace character(\n,\t,\v,\r,etc) in password.我想检测用户是否在密码中输入了任何空白字符(\n、\t、\v、\r 等)。 I created a function to detect using regex, and call it when user submits the form.
我创建了一个 function 来检测使用正则表达式,并在用户提交表单时调用它。
When i fetch the password value, javascript escapes the string escape squences's backslash.当我获取密码值时,javascript 会转义字符串转义序列的反斜杠。 I mean it convert \n to \\n, so my regex returns false.
我的意思是将 \n 转换为 \\n,所以我的正则表达式返回 false。 Here is my code:
这是我的代码:
let form = document.querySelector("form")
let inp = document.querySelector("#inp");
const hasWhitespace = (s)=>{
return /\s/.test(s)
};
form.addEventListener('submit',(e)=>{
e.preventDefault()
if(hasWhitespace(inp.value)){
alert("whitespaces not allowed ")
}
})
I have used replaceAll("\\\\","\\")
to replace double backslash into single but it didn't work.我已经使用
replaceAll("\\\\","\\")
将双反斜杠替换为单反斜杠,但它不起作用。
Thanks for your answer感谢您的回答
If you're restricting your user from entering any whitespace then you simply use.trim() method如果您限制用户输入任何空格,那么您只需使用.trim() 方法
like this let inp = document.querySelector("#inp").trim();
像这样
let inp = document.querySelector("#inp").trim();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.