0

I always forget how to create simple regular expressions despite doing projects with more complex regexes...

I have the string:

var s = "qwehref=\"1232.css\"qwasd asd asdqq eehref=\"asd.css\""

I want to capture the text inside the href tags: 1232.css and asd.css.

I've tried with this regex:

var re = /href="(.+\.css)"/g

but this is what I get (re.exec(s)):

'1232.css"qwasd asd asdqq eehref="asd.css'
0

2 Answers 2

5

try

/href="([^"]+\.css)"/g

You don't want to match the closing quote as part of your file name. Or just use a non greedy match I guess...

/href="(.+?\.css)"/g
Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to accept your answer because you have less reputation points :)
5

Greediness problem try

var re = /href="(.+?\.css)"/g

See it here on Regexr

The ? after a quantifier changes the matching behaviour of that quantifier to match as less as possible

Comments

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.