0

my regexp online address: https://regex101.com/r/JSPy5Z/1

I want to replace <link rel="stylesheet" href="/dist/abcd.css" /> using abcd.css content with an empty string.

input: <link ref="stylesheet" href="/dist/qwefqwefqwef.css" /><link ref="stylesheet" href="/dist/abcd.css" />

output: <link ref="stylesheet" href="/dist/qwefqwefqwef.css" />

enter image description here

CinCout
  • 9,486
  • 12
  • 49
  • 67
ycjcl868
  • 432
  • 3
  • 7
  • 18
  • I'm having a little bit of trouble understanding the question. Are you trying to capture the filename within the `src` attribute in a link element? So only the filename of the path? – John Doe Nov 06 '19 at 05:46
  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – apple apple Nov 06 '19 at 05:48
  • Do you want to capture all the `link` tags with `abcd.css`? – CinCout Nov 06 '19 at 05:48
  • replace all link tags with `abcd.css` – ycjcl868 Nov 06 '19 at 05:50
  • You can use the regular expression as follows //gm – Raju Nov 06 '19 at 05:50
  • @Raju not right using the regexp – ycjcl868 Nov 06 '19 at 05:54
  • @TheNewGuy I want to replace the `` tag with `href="/dist/abcd.css"` attr – ycjcl868 Nov 06 '19 at 06:00
  • Unfortunately, I don't belive this can be accomplished with just regex. You can certainly use regex to match/capture. You would most likely need a programming language for that, and given that you've tagged this question with `javascript`, the method you're looking for is [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace). – John Doe Nov 06 '19 at 06:03

2 Answers2

1

Use the following regex to match, then replace it with an empty string:

<link[^>]*href="\S*abcd\.css"[\s\S]*\/>

Two major changes:

  1. Your [^>] was misplaced. Now it captures only a single link tag.
  2. The capturing group is not required in this case, so I removed it.

Demo

CinCout
  • 9,486
  • 12
  • 49
  • 67
0

I changed the refs in your links to rels, which is the correct attribute name. If I understand what you're trying to do, this should work for you.

const
  links = `<link rel="stylesheet" href="/dist/qwefqwefqwef.css" /><link rel="stylesheet" href="/dist/abcd.css" />`,
  content = `abcd`, // set this to whatever you want to filter out
  re = RegExp(String.raw`<link rel="[^"]*" href="[^"]*?${content}\.css" />`);

console.log(`regular expression: ${re}`);
console.log(`output: ${links.replace(re, ``)}`);
Mason
  • 738
  • 7
  • 18