Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-31-2011, 11:11 PM   PM User | #1
anotherJEK
Regular Coder

 
Join Date: Aug 2010
Location: Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
Posts: 152
Thanks: 41
Thanked 1 Time in 1 Post
anotherJEK is an unknown quantity at this point
Odd RegExp behavior

I have been testing regex test string to match the following pattern
(- <any number with/without . decimal point>)
( -[#[.#]] )
here is the code that works the best:
Code:
var testStr = '99999+((-25.533) - 5)/99*(-25.533)';
var negValTestStr = new RegExp('\\(\-{1}[0-9]*\.?[0-9]*\\)', 'g');
var test = testStr.match(negValTestStr)
alert(test);
The question is: Why does it only work when the open and close
parenthesis are double escaped: '\\(' and '\\)'
When I use one backslash to escape, it will find -25.533, -5 and -25.533
With two backslashes for escape sequence: (-25.533), (-25.533)

Also, I have to escape the - to get just one -. If I do not escape the -,
-?; which should read - {0, 1} will match --# without escaping -

Thanks for thoughts on this
JK

Last edited by anotherJEK; 12-31-2011 at 11:16 PM..
anotherJEK is offline   Reply With Quote
Old 01-01-2012, 04:35 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 960
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
You need to escape the backslash for it to be interpreted as an escape character itself.
The RegExp constructor is intended for expressions that require the value of a changing variable or variables included within them. Yours could be constructed within //.
Logic Ali is online now   Reply With Quote
Users who have thanked Logic Ali for this post:
anotherJEK (01-01-2012)
Old 01-01-2012, 11:52 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,453
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If a regular expression is defined by wrapping it in / / characters then you only need to escape characters once.

If a regular expression is defined using RegExp then the expression starts as a text string and so any \ are string escapes that are applied before the string is converted to a regular expression. So any escapes you want to apply to the expression itself need to be double escaped - one for String and one for RegExp.

So in fact you are missing two backslashes in your expression in order for it to work correctly.


RegExp('\\(\\-{1}[0-9]*\\.?[0-9]*\\)', 'g');

or since that is a constant expression it is easier to define it as

/\(\-{1}[0-9]*\.?[0-9]*\)/g;
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:48 PM.


Advertisement
Log in to turn off these ads.