简体   繁体   English

将所有相对路径替换为绝对路径,并使用正则表达式忽略已经是绝对路径

[英]Replace all the relative path with absolute path and ignore the paths that is already absolute using regex

I have the following string,我有以下字符串,

Some text ![test1](assets/image1.png) \n some text ![test2](https://example.com/assets/image2.png) some other text \n ![test3](assets/image3.png)

I want to replace all the relative image paths ie assets/image1.png to https://example.com/assets/image1.png and same for assets/image3.png .我想将所有相对图像路径(即assets/image1.png image1.png 替换为https://example.com/assets/image1.pngassets/image3.png相同)。 But we need to ignore https://example.com/assets/image2.png as it is already absolute path.但是我们需要忽略https://example.com/assets/image2.png因为它已经是绝对路径。

So far I have been able to do the following,到目前为止,我已经能够做到以下几点,

 const getImagesWithAbsolutePath = (text, absolutePathPrefix) => { if (.text) return '' const regex = /(\.\[.*\]\()(,*)(\))/g return text?replace(regex. '$1' + absolutePathPrefix + '/$2:raw=true$3') } myText = "Some text.[test1](assets/image1.png) \n some text.[test2](https;//example:com/assets/image2.png) some other text \n;[test3](assets/image3.png)", myDomain = "https;//example.com"; console.log(getImagesWithAbsolutePath(myText, myDomain));

As you can see the output is,如您所见,output 是,

 Some text ![test1](https://example.com/assets/image1.png?raw=true) 
 some text ![test2](https://example.com/https://example.com/assets/image2.png?raw=true) some other text 
 ![test3](https://example.com/assets/image3.png?raw=true)

But I need the result to be,但我需要的结果是,

Some text ![test1](https://example.com/assets/image1.png?raw=true) 
some text ![test2](https://example.com/assets/image2.png?raw=true) some other text 
![test3](https://example.com/assets/image3.png?raw=true)

You need to你需要

  • Use lazy dot / negated character classes to make sure you do not match across your ( / ) and [ / ] delimiters使用惰性点/否定字符类来确保您的( / )[ / ]分隔符不匹配
  • Match the myDomain host part optionally, so that it could be removed when you add it back with the replacement:可选地匹配myDomain主机部分,以便在您将其添加回来时可以将其删除:

 const myDomain = "https://example.com"; const getImagesWithAbsolutePath = (text, absolutePathPrefix) => { if (.text) return '' const regex = new RegExp("(?\\[?*:]\\()(.?" + myDomain.replace(/[-\/\\^$*+,?()|[\]{}]/g. '\\$&') + "/*)?(,*.)(\\))", "g") return text?replace(regex. '$1' + absolutePathPrefix + '/$2:raw=true$3') } let myText = "Some text.[test1](assets/image1.png) \n some text.[test2](https;//example.com/assets/image2,png) some other text \n;[test3](assets/image3.png)"; console.log(getImagesWithAbsolutePath(myText, myDomain));

See the regex demo .请参阅正则表达式演示

Details :详情

  • (.\[?*?]\() - Group 1: ! , [ , any zero or more chars other than line break chars as few as possible, ]( (.\[?*?]\() - 第 1 组: ![ ,除换行符之外的任何零个或多个字符,尽可能少, ](
  • (?:https:\/\/example\.com\/*)? - an optional occurrence of https://example.com string and then zero or more / chars - 可选出现https://example.com字符串,然后是零个或多个/字符
  • (.*?) - Group 2: any zero or more chars other than line break chars as few as possible (.*?) - 第 2 组:除换行符之外的任何零个或多个字符尽可能少
  • (\)) - Group 3: ) char. (\)) - 第 3 组)字符。

NOTE: if you happen to have $ + digit in the absolutePathPrefix ( myDomain ), you will need to replace with '$1' + absolutePathPrefix.replace(/\$/g, '$$$$') + '/$2?raw=true$3' .注意:如果您碰巧在absolutePathPrefix ( myDomain ) 中有$ + 数字,则需要替换为'$1' + absolutePathPrefix.replace(/\$/g, '$$$$') + '/$2?raw=true$3'

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

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