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;

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 "".

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>

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

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>

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.

thanx a lot. seems to be working with all the backslashes in place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.