• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About Java Script Regular Expressions

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic