[英]how to check whether any special character is present in a particular string
I want to remove the '<' '>' from the string so i used the script 我想从字符串中删除'<''>',所以我使用了脚本
<html>
<body>
<script>
var f = ((/(<script>)/i).exec("hello<") != null);
alert("ok"+f);
document.write("<br>Value:" +f);
</script>
</body>
</html>
But i'm getting the variant f value as null.. 但是我将变量f值设置为null。
Could you please help me.if there is any issue with the code. 如果代码有任何问题,请您帮我。
I hope this will help 我希望这个能帮上忙
if("hello<".indexOf('<') != -1){
var f = "hello<".replace('<','');
alert("ok "+f);
}
You can you JavaScript regular expressions to achieve this. 您可以使用JavaScript正则表达式来实现此目的。
"<script>".replace(/<|>/ig, "");
outputs: "script"
var f = ('<script>'.replace(/<|>/g, ''));
alert("ok"+f);
document.write("<br>Value:" +f);
You can use .test
method: 您可以使用
.test
方法:
var originalString = '<script>',
r = originalString.test(/<|>/g, ''); // returns true or false
if (r === true) {
//whatever you wanna do if you have special characters in
// your string
} else {
// no special chars - all clean!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.