开发者

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:

  1. change str.replace to url.replace

  2. remove the ' around the regex

    var 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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜