简体   繁体   English

麻烦在正则表达式中使用string.replace

[英]trouble using string.replace with regex

Given something a regex like this: 给定这样的正则表达式:

http://rubular.com/r/ai1LFT5jvK http://rubular.com/r/ai1LFT5jvK

I want to use string.replace to replace "subdir" with a string of my choosing. 我想使用string.replace用我选择的字符串替换“ subdir”。

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

only returns the same string, as shown here: http://jsfiddle.net/nLmbV/ 仅返回相同的字符串,如下所示: 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. 如果您查看Rublar,它似乎可以捕捉到我想要捕捉的东西,但是在Fiddle上,它并不能替代它。

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. 正确的正则表达式或replace调用的正确实现会很好,但是最重要的是,我想了解自己做错了什么,以便将来可以避免。

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 根据小提琴,它只是返回hello而不是https://xxxxxxxxxxx.cloudfront.net/dir/hello/Slide1_v2.PNG

It's that little \\z in your regex. 正则表达式中只有那个\\z

You probably forgot to replace it with a $ sign. 您可能忘了用$符号代替它。 JavaScript uses ^ and $ as anchors, while Ruby uses \\A and \\z . JavaScript使用^$作为锚点,而Ruby使用\\A\\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. 如果不编写极其复​​杂的正则表达式,使用正则表达式几乎不可能可靠地处理此答案URL的原因。 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. 如果在浏览器中执行此操作,则可以在脚本中使用A标记,以简化操作。 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: A标签知道如何将它们解析为小块,它使您可以独立地修改小块,因此您只需要处理路径名:

//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. a.href现在包含所需的URL。 More lines, but also more hair left when you come back to change the code later. 当您回来以后更改代码时,会保留更多的行,但还会留下更多的头发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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