Home > Mobile >  Regex extract string from svg attribute
Regex extract string from svg attribute

Time:01-09

I want to extract the filter name from SVG elements.

I have this text string: var filter = "url(#myFilter)";

I want to extract only the text in parentheses, without the # sign.

In this case the desired response is: "myFilter"

Can you help me with the corresponding Regex

CodePudding user response:

We can use match here as follows:

var input = 'var filter = "url(#myFilter)";';
var url = input.match(/\(#(.*?)\)/)[1];
console.log(url);

CodePudding user response:

Try this:

(?<=\(#)[^()] (?=\))
  • (?<=\(#) a position where it is preceded by a (#, the (# is not included in the match.
  • [^()] one or more character except ( and ).
  • (?=\)) a position where it is followed by a ), the ) is not included in the match.

See regex demo

CodePudding user response:

var filter = "url(#myFilter)";
var regex = /\(([^#][^)] )\)/;
var match = filter.match(regex);
if (match) {
  var text = match[1];  // it will filter "myFilter"
}

You can try this, Hope it will helps

  • Related