1

I am using regex to match css values.

Input string to match

 font-size:25px;font-family:georgian;content:"' unicode given in pseudo © '";

Regex I am using to match

/.*\bcontent:(\s*[^;]*)/

I am using $1 to get the actual string output of regex

Expected output is "' unicode given in pseudo &#169 '"**

But actual is "' unicode given in pseudo &#169

Because regex finds ; in the content value so it breaks there. So how can i fix this bug. Ideally regex should look for the last ; which is not inside the double/single quotes. Because my input string will always have ; after each property value ends.

Thanks

2
  • Try /.*\bcontent:\s*(.*);/ to match up to the last ; char on a line. Or just /\bcontent:\s*"([^"]*)";/ (demo) Commented Oct 16, 2018 at 9:40
  • Try \bcontent:\s*(".*?");, see this demo. Commented Oct 16, 2018 at 11:51

1 Answer 1

2

Simplify it: Take the piece of text within quotes:

/.*\bcontent:(\"[^\"]*\")/
Sign up to request clarification or add additional context in comments.

7 Comments

If one wants to simplify the pattern, one would omit .* in the pattern. As in my comment, it is enough to use /\bcontent:("[^"]*")/ (I just placed the capturing group inside the quotes to exclude them from the match).
its failing for this case content:"'@@@@@@@@@@@@@ '","'@@@@@@@@@@@@@&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'","'@@@@@@@@@@@@@&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'";
@WiktorStribiżew This works but not for all the cases.Ideally regex should look for the last ; which is not inside the double/single quotes. Because my input string will always have ; after each property value ends.
@Pavan, of course it fails, there's no 'content' in your string.
@PavanTiwari You cannot get an undefined number of quoted substrings until the first non-quoted semicolon with a regular expression. You need to code an specific parser for that matter.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.