JS USES regular expressions to remove duplicate characters in a string
- 2020-10-07 18:35:08
- OfStack
This article illustrated how JS USES regular expressions to remove duplicate characters in a string. To share for your reference, the details are as follows:
Here is a simple example of an JavaScript regular expression that filters out the redundant characters in a string that contains a duplicate string. Run it to see what happens.
The specific code is as follows:
<html>
<head>
<title> Use regular representation to remove duplicate characters in a string </title>
</head>
<body>
<script language="javascript">
str = "Google"
str1 = str.replace(/(.).*\1/g,"$1")
document.write(str + "<br>");
document.write(str1);
</script>
</body>
</html>
The operation results are as follows:
Google
Gogle
PS: Here are two more handy regular expression tools for you to use:
JavaScript Regular Expression online test tool:
http://tools.ofstack.com/regex/javascript
Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg
I hope this article has been helpful for JavaScript programming.