Introduction:
Here I will explain how to use JavaScript regular expression to remove special characters or regular expression to escape special characters in JavaScript or regex to replace special characters in string using JavaScript. By using JavaScript replace function we can replace or remove special characters in string.
Description:
In previous articles I explained jQuery remove whitespaces from string with regex, jQuery load more records on button click in repeater, jQuery show asp.net exception message in ajax calls, jQuery check whether radiobutton selected or not, jQuery get radio button selected value by name, jQuery validate radiobuttonlist with example and many articles relating to css, AngularJS, jQuery, JavaScript and asp.net. Now I will explain how to remove or escape special characters using regular expression in JavaScript with example.
In previous articles I explained jQuery remove whitespaces from string with regex, jQuery load more records on button click in repeater, jQuery show asp.net exception message in ajax calls, jQuery check whether radiobutton selected or not, jQuery get radio button selected value by name, jQuery validate radiobuttonlist with example and many articles relating to css, AngularJS, jQuery, JavaScript and asp.net. Now I will explain how to remove or escape special characters using regular expression in JavaScript with example.
To replace special characters with
regular expression we need to write the code like as shown below
<script type="text/javascript">
function escapecharacters() {
var str =
document.getElementById('txtDetails').value;
// It will replace all characters except alphabets,
characters. and space
alert(str.replace(/[^a-zA-Z0-9-. ]/g, ""));
}
</script>
|
In
above code “^” symbol in regular
expression will leave mentioned characters and replace or remove other
characters.
If
you want to check it in complete example you need to write the code like as
shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Javascript regular
expression to replace (Escape) special characters</title>
<script type="text/javascript">
function escapecharacters() {
var str =
document.getElementById('txtDetails').value;
// It will replace all characters except alphabets,
characters. and space
alert(str.replace(/[^a-zA-Z0-9-. ]/g, ""));
}
</script>
</head>
<body>
<form id="form1">
<div>
Enter Message:<input type="text" id="txtDetails" value="hello' welcome to#$% aspdotnet-@!suresh.com"
/>
<input type="button"
id="btnRemove"
value="Remove
Characters" onclick="escapecharacters()" />
</div>
</form>
</body>
</html>
|
Live
Demo
For live demo click on below button by entering text in
textbox with special characters
Enter Message:
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.