wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes About Java Script Regular Expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "About Java Script Regular Expressions" Watch "About Java Script Regular Expressions" New topic
Author

About Java Script Regular Expressions

Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 27, 2008
Posts: 46
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 08, 2001
Posts: 15376
    
    6


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

Joined: Jan 27, 2008
Posts: 46
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Java Script Regular Expressions
 
Similar Threads
HELP!!!! with Java Regular Expressions
How to Read Param value from web.xml
I don't think I understand how Regular Expression works!
Calender class doubt
Iterating through a String.