Home  >  Article  >  Web Front-end  >  JS method to use regular expressions to remove repeated characters in a string_javascript skills

JS method to use regular expressions to remove repeated characters in a string_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:33:401970browse

本文实例讲述了JS使用正则表达式除去字符串中重复字符的方法。分享给大家供大家参考,具体如下:

这里演示一个简单的JavaScript正则表达式实例,将一串含有重复字符串中的多余字符滤除掉,请运行查看效果。

具体代码如下:

<html>
<head>
<title>利用正则表达法除去字符串中的重复字符</title>
</head>
<body>
<script language="javascript">
  str = "Google" 
  str1 = str.replace(/(.).*\1/g,"$1") 
  document.write(str + "<br>");
  document.write(str1);
</script>
</body>
</html>

运行结果如下:

Google
Gogle

希望本文所述对大家JavaScript程序设计有所帮助。

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