Loading ...

Escaping single quote with regex | CodeAsp.Net

Escaping single quote with regex

 /5
0 (0votes)

In this blog I will explain how to escape single quotes with regular expression in javascript. There are issues when we have  single quotes with in string in javascript. Below is the code snippet.

Escaping only one single quote.

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" >

<title></title>

</head>

<body>

<div>

</div>

</body>

 

<script type="text/javascript">

 

var str = 'Te\'st';

var regx = /\'/;

alert('Old Value:' + str);

str = str.replace(regx, '');

alert('New Value:' + str);

 

</script>

 

</html>




Escaping all single quotes.

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title></title>

</head>

<body>

<div>

</div>

</body>

 

<script type="text/javascript">

 

var str = 'T\'\'\'est';

var regx = /\'/g;

alert('Old Value:' + str);

str = str.replace(regx, '');

alert('New Value:' + str);

 

</script>

 

</html>

 


So above we discussed how to escape single quotes with in string through regular expression.
Do let me know your feedback, comments.


Comments (no comments yet)

Top Posts