Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 would like to use regex to replace each occurrence of the code thing with a link to it. The result of above example would be then,

The <A HREF='http://server.com/XX00123'>XX00123</A> is a sibling of <A HREF='http://server.com/XX00456'>XX00456</A> and parent of <A HREF='http://server.com/XX00789'>XX00789</A>

I think I could do it with

  1. put the search result in an array,
  2. if the array is not empty, construct the text string of the link for each element in the array,
  3. do a loop to find each occurrence again and replace it with the link

But is there any way to do this regular expression just in one line with JavaScript and/or JQuery?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
172 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use the following here.

var result = str.replace(/(XXd+)/gi, "<A HREF='http://server.com/$1'>$1</A>");

Working Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...