anotherJEK
12-31-2011, 11:11 PM
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:
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
(- <any number with/without . decimal point>)
( -[#[.#]] )
here is the code that works the best:
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