0

I have a search box. I need to remove all the special characters from the search term and then query it. “’-,?~!@#$%&*+-= all these characters.... can anybody suggest a proper regular expression for this ? and the syntax for using it in javascript.
my query is saved in var query;

3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by Jayakrishnan_gk
0

ya. i know how to use replace() function. Its just that i dont know how to create a regular expression corresponding to !@#$%&*,"'
i need a regular expression that matches any occurrence of these characters in the string and replace it by "".

0

You can create a regular expression like the one in the variable called myRegExPattern to match any character listed between the square brackets. The square brackets define a "character class" aka "character set". Then replace each character that is matched.

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!-- Hide from browsers that do not support JavaScript
var myRegExPattern = /[\-,?~!@#$%&*+\-'="]/g
var query = "Then it's one, two, three strikes you're out at the old, ball-game!..."
query = query.replace(myRegExPattern, "");
document.writeln(query)
// --> Finish hiding
</SCRIPT>
0

I tried the above regular expression. But the " and ' characters are not getting replaced.

0

That's strange. I tested the following and the same RegEx did remove my single and double quotes. I modified the script so you can type in any text you want in the prompt box.

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

<!-- Hide from browsers that do not support JavaScript
var myRegExPattern = /[\-,?~!@#$%&*+\-'="]/g
var query = "Let's test to see if 'single quotes' and \"double quotes\" are removed.";
query = prompt("Please enter your query",query);
var cleanquery = query.replace(myRegExPattern, "");
alert("Cleaned query is: " + cleanquery);

document.writeln("Original query was:<br>")
document.writeln(query)
document.writeln("<p></p>")
document.writeln("Cleaned query is:<br>")
document.writeln(cleanquery)

// --> Finish hiding

</SCRIPT>

I'm testing on a Windows Vista system with regional settings for North America. If you are using a different platform with different regional settings maybe that would give you different results.
Would it be simpler to make a RegEx pattern for the characters that you do want in your query? If you want only alphanumeric characters and spaces in your query you could make a Regex to find any character not in the specified character set. For example:

<HTML>
<HEAD>
<TITLE>Testing RegEx in Javascript</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

<!-- Hide from browsers that do not support JavaScript
var myRegExPattern = /[^\w\s]/g
var query = "Let's test to see if 'single quotes' and \"double quotes\" are removed.";
query = prompt("Please enter your query",query);
var cleanquery = query.replace(myRegExPattern, "");
alert("Cleaned query is: " + cleanquery);

document.writeln("Original query was:<br>")
document.writeln(query)
document.writeln("<p></p>")
document.writeln("Cleaned query is:<br>")
document.writeln(cleanquery)

// --> Finish hiding

</SCRIPT>
</HEAD>

<BODY>
<p> HTLM body text here.</p>
</BODY>
</HEAD>
</HTML>
0

One more suggestion: if you still want to use the RegEx pattern that specifies each punctuation character you want to remove, try preceding the " and the ' characters with a backslash in the RegEx pattern, like this: var myRegExPattern = /[\-,?~!@#$%&*+\-\'=\"]/g On my platform it doesn't make any difference, it matches and replaces the quotes either way, but might as well try it. (Notice that I had to escape the hyphen - character because otherwise it has a special meaning in RegEx character classes.)

Good luck. I'm learning about javascript today by looking at the tutorial at http://www.w3schools.com/js/default.asp. Their "Try It Yourself" button is handy for testing something quickly.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.