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.

Thanks.:)

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