I am new to JavaScript.

Therefore, I don't understand how the following javascript code work:

var specialChars="/\\(\\)><@,;:\\\\\\\"\\.\\[\\]\*\+\?\!\#\$\%\^\&\=\~\`\|\/\'";
var validChars="\[^\\s" + specialChars + "\]";
var atom=validChars + '+';
var atomPat=new RegExp("^" + atom + "$");
var str = "Hello World!";

if (str.search(atomPat)==-1) 		
    alert ("Special Characters Found!"); 

When the code run, it will prompt the alert message, which I don't understand why.:-/
Could someone please kindly explain.


7 Years
Discussion Span
Last Post by jomanlk

The script just concatenates a lot of characters together and then searches the "Hello World" string for the original string you created.

I think the original intention was to create a regular expression of valid characters and search for those characters in a string you input but that went horribly wrong.

For example, if you wanted a number regular expression all it takes is a single line to create it and another line to test it.

numReg    = new RegExp(/^[0-9]+$/);
alert(numReg.test("sdsd")) //Alerts false since it's not a number
alert(numReg.test(22)) //alerts true.
This topic has been dead for over six months. 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.