This works almost good, but it fails on the 37:1 line. I’m not sure how I can modify my regex to wrap the 37 in a span, and 1 in a span. So e.g. my output should be
I technically should account for decimals as well. I did not notice that part missing. I updated my codepen since I posted this thread, which was a mistake. I should have made a new one to test James’ code.
You’re brilliant, thank you . I’ve always struggled with regex. It’s just a daunting bit of code. Is there a really dumbed down tutorial you recommend? I’ve read several over the years but it makes my eyes roll and makes me give up .
Edit - For example, comparing your last bit of regex to this, it’s such a big difference.
Nah, not really. For this second example, I just googled “JavaScript regex to match integer or decimal” and landed here:
The first answer does what we are after so I just used that
If you read that answer, it breaks the regex down and explains it really well. Let me know if there’s anything you don’t understand after reading that.
Not really, I’m afraid. I have just picked up bits and pieces over the years and usually know enough to manage to achieve whatever it is that I am trying to do. I don’t think I ever sat down and read a tutorial.
We have this, over on the main site. This might be a good starting point if you are looking for a refresher.
Nah man, as I understood it, Ryan is trying to wrap spans around any integers (so 2 becomes <span>2</span>), but he is also trying to wrap spans around decimals (so 2.0 becomes <span>2.0</span>). Under this assumption, the results posted above were correct.
(Ignoring the outer pattern parenthesis, because they’re fairly irrelevant and could be replaced by using $0 in the replacement.)
In “english”:
“A number that is not 0, followed by any number of digits; possibly followed by the combination of: a single period and then one or more digits”
I agree, gold works much better than yellow, and I find that replacing orange for darkorange in my crayon set makes for easier reading too. https://colours.neilorangepeel.com/