Home > Net >  Regex find specific string between two other strings
Regex find specific string between two other strings

Time:12-18

I have a .html with obfuscated class names, something like this . I want to replace only one specific classnames(for example "hk") in the entire html.I need a regex expression that can help me achieve that.

I have found this expression: (?<=]*).*(?=")

So I tried the following: (?<=]*)(.*hk.*)(?=") but it returns also all the classes within the same body.Result here

It should return the hk word alone

Any help is apreciated.

CodePudding user response:

Mandatory Surgeon General warning: you shouldn't use regex to parse HTML.

In this very limited case, you might use

(<[^>]*class\s*=\s*"[^"]*)\bhk\b([^"]*")

and replace it with \1whatever\2. Then "hk" becomes "whatever" provided it's inside a class inside a HTML tag. The \b specifier will prevent "ohka" from matching and the \s ensures that "class = " and "ab hk cd" style="display: block">

You can experiment using https://regex101.com .

  • Related