Moose
Moose

Reputation: 1241

Regex select everything between two strings, but not one of the strings

I have some html code that I need to regex and remove a part of it.

In this particular example I need to remove 2-columns.. The full string is name="2-columns.Heading-1" but needs to read name="Heading-1"

I would like this to be extendable because another example could be name="3-columns.Heading-1". So I want to remove everything after the starting point of name=" and the ending . (I also want to remove the .)

Can anyone help me form the correct regex? I'm struggling.

Upvotes: 0

Views: 227

Answers (3)

vks
vks

Reputation: 67988

[^"]*\.

Try this.Replace by empty string.See demo.

http://regex101.com/r/dZ1vT6/24

Upvotes: 1

epoch
epoch

Reputation: 16615

Replace at group 1:

/name="(([^.]+)\.)Heading-1"/

Or:

'<tag name="2-columns.Heading-1">lalalala</tag>'.replace(/name="(([^.]+)\.)Heading-1"/, 'name="Heading-1"')

is:

"<tag name="Heading-1">lalalala</tag>"

and:

'<tag name="2-columns.Heading-2">lalalala</tag>'.replace(/name="(([^.]+)\.)Heading-1"/, 'name="Heading-2"')

is:

"<tag name="2-columns.Heading-2">lalalala</tag>"

or:

'<tag name="2-columns.Heading-1">lalalala</tag>'.replace(/(name=")(([^.]+)\.)(?=Heading-1")/, "$1");

Upvotes: 0

Alex K.
Alex K.

Reputation: 175956

One way;

str = str.replace(/(^name=\")(.*\.)/, "$1");

Upvotes: 0

Related Questions