javascript regular expression replacement doesnt work?
why this regular expression replacement doesnt work?
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace('/page\/[0-9]+/', 'page/2'); //it m开发者_运维知识库ust become http://myweb.com/page/2/id/2
You need to do two things:
change
str.replace
tourl.replace
remove the
'
around the regexvar url = 'http://myweb.com/page/1/id/2'; newUrl = url.replace(/page\/[0-9]+/, 'page/2');
Example: http://jsfiddle.net/fhqXn/
Use url
rather than str
, if you want to replace something in the String stored in the url
variable.
You have a naming misspell.
Rename your url
var to str
or change the str.replace
to url.replace
:
newUrl = url.replace('/page\/[0-9]+/', 'page/2');
精彩评论