简体   繁体   English

如何检查特定字符串中是否存在任何特殊字符

[英]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.

相关问题 检查字符串是否在javascript中包含任何特殊字符或字符串 - checking whether a string contains any special character or string in javascript 如何检查JavaScript中是否存在区分大小写和特殊字符的变量中的特定字符串? - How to check if particular string is present in variable with case sensitive and special characters in javascript? 如何检查json字符串中是否存在NAN - how to check whether NAN is present in json string or not 如何检查字符串中的字符是否是 JavaScript 中的特定字符? - How do I check whether a character in a string is a specific character in JavaScript? 如何检查字符串中的某个字符是否在另一个字符之后? - How to check whether a certain character in string comes after another character? 使用should.js如何检查数组中是否存在字符串? - Using should.js how to check whether a string is not present in an array? 如何使用lodash js检查字符串是否存在于数组中? - How to check whether string is present in array with lodash js? 当用户单击链接时,如何检查GridHyperLink的NavigateUrl中是否存在特定文本? - How to check whether the particular text present in GridHyperLink's NavigateUrl, when the user clicks on the link? 如何使用javascript传递带/或任何其他特殊字符的字符串 - how to pass string with / or any other special character using javascript 如何编写正则表达式来检查字符串是否包含特殊字符和多个空格 - How to write regex to check if a string contains special character and multiple whitespaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM