Regular Expression Replace Multiple Matching Pattern With Link
I have text fields that contains one or multiple occurrence of some code like 'XX00123' or 'XX00456', for example, The XX00123 is a sibling of XX00456 and parent of XX00789 I wou
Solution 1:
You can use the following here.
var result = str.replace(/\b(XX\d+)\b/gi, "<A HREF='http://server.com/$1'>$1</A>");
Solution 2:
You could use something like this:
var re = /([X0-9]+)/gi;
var result = str.replace(re, "<A HREF='http://server.com/$1'>$1</A>");
The regex
will match one or more x
followed by numbers until a space or a non-numeric value.
Solution 3:
With the following code,
var result = str.replace(/(XX(\d{5}))/ig, "<A HREF='server.com/$2'>$1</A>");
The result would be,
The <AHREF='http://server.com/00123'>XX00123</A> is a sibling of <AHREF='http://server.com/00456'>XX00456</A> and parent of <AHREF='http://server.com/00789'>XX00789</A>
The $1 represents "(XX(\d{5}))" - the out side pair of parentheses, and the $2 represents "(\d{5})" - the inside pair of parentheses.
Post a Comment for "Regular Expression Replace Multiple Matching Pattern With Link"