[英]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, 我的文本字段包含一个或多个“ XX00123”或“ XX00456”之类的代码,例如,
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 我想我可以做到
But is there any way to do this regular expression just in one line with JavaScript and/or JQuery? 但是,有什么方法可以仅使用JavaScript和/或JQuery来完成此正则表达式吗?
You can use the following here. 您可以在此处使用以下内容。
var result = str.replace(/\b(XX\d+)\b/gi, "<A HREF='http://server.com/$1'>$1</A>");
With the following code, 使用以下代码,
var result = str.replace(/(XX(\d{5}))/ig, "<A HREF='server.com/$2'>$1</A>");
The result would be, 结果是
The <A HREF='http://server.com/00123'>XX00123</A> is a sibling of <A HREF='http://server.com/00456'>XX00456</A> and parent of <A HREF='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. $ 1表示“((XX(\\ d {5}))””-括号的外侧,而$ 2表示“(\\ d {5})”-括号的内侧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.