Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Java Script Regular Expressions

 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to Java Script.I wrote a code like...

var reg=new RegExp("/^13[\d]{9}$/");
document.writeln(reg.test("13123456789"));

I am getting "false" as result. Can anybody say me what is happening here? Why the string in test() function don't match with this pattern? and also give an example string, which matches this pattern.

Thanks in advance..

--Shashi
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use RegExp, you do not have to include / at the beginning or at the end. If you use \, you need to escape it.

With that said, your regular expression would look like this:


Now if you want to limit the keystrokes and avoid using RegExp, which is slower, you can do something like this:


Eric
 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Eric,

Now I tried using the code as you said, still I am getting "false" only. What would be the problem??

And I am having one more doubt too..

code
----------------------------------------------------
var reg=/[0-9a-zA-Z\._-]/;
document.writeln(reg.test("abcd&%"));
document.writeln(reg.test("&%"));
----------------------------------------------------

In case of first writeln() function, I am getting "true" as result(But my expected result is "false"). And in the second writeln() function I am getting "false".

Can you please say me what actually the pattern /[0-9a-zA-Z\._-]/ means?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran this:


And I get
true
true

and

false
false

So they work for me. You copying the code right?

Eric
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The first one is true because it does make a match. It is matching abcd. You did not tell the regular expression it had to match the entire string. You said it has to match any of those characters.

If you would add start of string [^] and end of string [$] to it, plus say it has to be there one or more time [+]
var reg=/^[0-9a-zA-Z\._-]+$/;

You would get false because it has to match the whole thing. What does it mean? It has to match any of those characters.

I really think you need to do some reading on this stuff.

Here is an article on them: http://www.webreference.com/js/column5/

Eric
 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Eric,

I tried the code as you said. I got results good. Again Thank you for sending me a link to learn regular expressions.


--------
Shashi Kala
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic