I'd like to match every dot or comma but not in href attribute. So I have this regular expression:
^(?!.*?href=)(.*?)([.,])(\S+)
But it matches only the first occurrence. I think it because of non-greedy .*?
But I can't come up with anything else. Can you help me, please?
Asked
Active
Viewed 354 times
0

klinx
- 13
- 2
-
Can you provide an example of a string and the parts you'd like to match? – Axnyff Aug 04 '18 at 10:34
-
Flavor? C#? JavaScript? – JohnyL Aug 04 '18 at 10:37
-
Pull out the text first and then match it. You can’t parse HTML with regexes: https://stackoverflow.com/a/1732454/8492116 – Aankhen Aug 04 '18 at 10:41
2 Answers
0
What you might do to match every dot or comma and assuming that the attribute value is between single or double quotes is to match what you don't want and to capture in a group what you want to keep.
If you don't want to match a dot in the href you could match it with href="
followed by [^"]*"
or '[^']*'
. Then you could use an alternation |
to capture in a group a dot or a comma using ([.,])

The fourth bird
- 154,723
- 16
- 55
- 70
-
1A good example of [The Best Regex Trick Ever](https://www.rexegg.com/regex-best-trick.html#thetrick). +1 – Wiktor Stribiżew Sep 20 '19 at 09:35
-1
If you want to match every occurrence, you will need to run the regex with the global (g) flag: e.g.
/^(?!.*?href=)(.*?)([.,])(\S+)/g
I suggest you use a tool such as https://regex101.com/ to test and debug your regular expressions, it's super handy!

James Skirving
- 64
- 4
-
This is not a good approach. It simply [can't work](https://regex101.com/r/ljMMDL/1) even on the regex tester page. – Wiktor Stribiżew Sep 20 '19 at 09:37