Home  >  Article  >  Web Front-end  >  Use JS regular expressions to replace brackets, angle brackets, etc.

Use JS regular expressions to replace brackets, angle brackets, etc.

高洛峰
高洛峰Original
2016-12-03 15:28:021684browse

As shown below:

function toTxt(str) { 
  var RexStr = /\<|\>|\"|\&#39;|\&/g 
  str = str.replace(RexStr, function(MatchStr) { 
    switch (MatchStr) { 
    case "<": 
      return "<"; 
      break; 
    case ">": 
      return ">"; 
      break; 
    case "\"": 
      return """; 
      break; 
    case "&#39;": 
      return "&#39;"; 
      break; 
    case "&": 
      return "&"; 
      break; 
    default: 
      break; 
    } 
  }) 
  return str; 
}


The above article uses JS regular expressions to replace brackets, angle brackets, etc. This is all the content shared by the editor


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn