By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

regExp problem

P: n/a
Hi, all
I tried this and it works.
------
var s1=" xxxx ";
alert(">>"+s1.replace(/^\s+/, " ")+"<<");
But why this won't work
------
var s1=" xxxx";
var reg=new RegExp("^\s+");
alert(">>"+s1.replace(reg," ")+"<<");

jack
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Lee
datactrl said:
But why this won't work var reg=new RegExp("^\s+");


The value of the string being passed to the RegExp constructor
is "^s+". The backslash is consumed as the literal string is
parsed. If you want the string to contain a backslash, you
have to escape it by preceding it with another backslash:

new RegExp("^\\s+");

or simply use a RegExp literal:

var reg=/^\s+/;

Jul 23 '05 #2

P: n/a
Thanks Lee. It works.

Jack
"Lee" <RE**************@cox.net> wrote in message
news:c8*********@drn.newsguy.com...
datactrl said:
But why this won't work

var reg=new RegExp("^\s+");


The value of the string being passed to the RegExp constructor
is "^s+". The backslash is consumed as the literal string is
parsed. If you want the string to contain a backslash, you
have to escape it by preceding it with another backslash:

new RegExp("^\\s+");

or simply use a RegExp literal:

var reg=/^\s+/;

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.