简体   繁体   中英

trouble using string.replace with regex

Given something a regex like this:

http://rubular.com/r/ai1LFT5jvK

I want to use string.replace to replace "subdir" with a string of my choosing.

Doing myStr.replace(/^.*\\/\\/.*\\.net\\/.*\\/(.*)\\/.*\\z/,otherStr)

only returns the same string, as shown here: http://jsfiddle.net/nLmbV/

If you view the Rublar, it appears to capture what I want it to capture, but on the Fiddle, it doesn't replace it.

I'd like to know why this happens, and what I'm doing wrong. A correct regex or a correct implementation of the replace call would be nice, but most of all, I want to understand what I'm doing wrong so that I can avoid it in the future.

EDIT

I've updated the fiddle to change my regex from:

/^.*\/\/.*\.net\/.*\/(.*)\/.*\z/

to

 /^.*\/\/.*\.net\/.*\/(.*)\/.*$/

And according to the fiddle, it just returns hello instead of https://xxxxxxxxxxx.cloudfront.net/dir/hello/Slide1_v2.PNG

It's that little \\z in your regex.

You probably forgot to replace it with a $ sign. JavaScript uses ^ and $ as anchors, while Ruby uses \\A and \\z .


To answer your edit:

The match is always replaced as a whole. You'll want to group both the left side and the right side of the to-be-replaced part and reinsert it in the replacement:

url.replace(/^(.*\/\/.*\.net\/.*\/).*(\/.*)$/,"$1hello$2")

Before I get marked down, I know the question asks about regexp. The reason for this answer URLs are nearly impossible to process reliably with a regexp without writing fiendishly complex regexps. It can be done, but it makes your head hurt! If you are doing this in a browser, you can use an A tag in your script to make things much simpler. The A tag knows how to parse them into pieces, and it lets you modify the pieces independently, so you only need to deal with the pathname:

//make a temporary a tag
var a = document.createElement('a');
//set the href property to the url you want to process
a.href = "scheme://host.domain/path/to/the/file?querystring"
//grab the path part of the url, and chop up into an array of directories
var dirs = a.pathname.split('/');
//set 2nd dir name - array is ['','path','to','file']
dirs[2]='hello';
//put the path back together
a.pathname = dirs.join('/');

a.href now contains the URL you want. More lines, but also more hair left when you come back to change the code later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM