-1

I'd like to surround the 2.5 with a span elemtent using regular expressions preg_replace. But only in the link name, not the URL.

<a href="mydomain.tld/2.5-Subchapter.php">2.5 Subchapter</a>

Anyone likes to help me with this challenge?

I did try this Regex validation on decimal already, but it didn't work for me.


Edit:

Thanks to ssc-hrep3's answer below, here the answer for a PHP solution (I forgot to mention it before):

$myNav = preg_replace('/(>)(\d\.\d+)/s', '$1<span>$2</span>',$myNav ); 

Kind Regards, Steffano

Community
  • 1
  • 1
Steffano
  • 43
  • 7

1 Answers1

2

If you have just this simple case, you could look for the > character:

(>)(\d\.\d)

And replace it with:

$1<span>$2</span>

var text = '<a href="mydomain.tld/2.5-Subchapter.php">2.5 Subchapter</a>';
var regex = /(>)(\d\.\d)/g;
var replacement = "$1<span>$2</span>";

var result = text.replace(regex, replacement);
console.log(result);
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87