JavaScript / jQuery字符串替换为正则表达式

时间:2021-07-23 21:45:25

Let's say I retrieve the value of a <textarea> using jQuery. How can I then replace a portion of the value using JavaScript/jQuery. For example:

假设我使用jQuery检索

string: "Hey I'm $$zach$$"

字符串:“嘿,我是$$ zach $$”

replace $$zach$$ with <i>Zach</i>

Zach 替换$$ zach $$

And still keep the rest of the string intact?

并保持其余的字符串完好无损?

4 个解决方案

#1


33  

Use a regex replace:

使用正则表达式替换:

yourTextArea.value = yourTextArea.value.replace(/\$\$(.+?)\$\$/, '<i>$1</i>')

Explanation of the regex:

正则表达式的解释:

\$\$  two dollar signs
(     start a group to capture
.     a character
+     one or more
?     lazy capturing
)     end the group
\$\$  two more dollar signs

The capture group is then used in the string '<i>$1</i>'. $1 refers to the group that the regex has captured.

然后在字符串' $ 1 '中使用捕获组。 1美元是指正则表达式捕获的组。

#2


9  

Use this:

用这个:

str.replace(/\${2}(.*?)\${2}/g, "<I>$1</I>");
\${2} matches two $ characters
(.*?) matches your string to be wrapped
\${2} same as above
/g matches globally

jsFiddle

的jsfiddle

If you wanted something in jQuery:

如果你想要jQuery中的东西:

$("#txt").val().replace(/\${2}(.*?)\${2}/g, "<I>$1</I>");

Markup:

标记:

<textarea id="txt">I'm $$Zach$$</textarea>

jsFiddle

的jsfiddle

Wrap it in a function for best use:

将其包装在一个功能中以便最佳使用:

var italics = function (str) {
    return str.replace(/\$\$(.*?)\$\$/g, "<I>$1</I>");
}

italics($("#txt").val());

Seems like you want to make a syntax similar to Markdown. Why not just use a Markdown parser for your fields instead of reinventing the wheel?

好像你想要制作类似于Markdown的语法。为什么不在你的领域使用Markdown解析器而不是重新发明*呢?

Showdown JS is actively developed and you get the same Markdown syntax as with any other markdown syntax.

Showdown JS正在积极开发中,您可以获得与任何其他markdown语法相同的Markdown语法。

#3


3  

Using the string .replace method will do.

使用字符串.replace方法可以。

.replace(/\$\$(.*?)\$\$/g, '<I>$1</I>')

#4


2  

Use this, change link and tag for extended linkify function :

使用此选项,更改链接和标记以扩展linkify功能:

String.prototype.linkify = function() {
    var wikipediaPattern = /<wikipedia>(.+?)<\/wikipedia>/g; 
    return this.replace(wikipediaPattern, '<a href="http://fr.wikipedia.org/wiki/$1">$1</a>');
}

#1


33  

Use a regex replace:

使用正则表达式替换:

yourTextArea.value = yourTextArea.value.replace(/\$\$(.+?)\$\$/, '<i>$1</i>')

Explanation of the regex:

正则表达式的解释:

\$\$  two dollar signs
(     start a group to capture
.     a character
+     one or more
?     lazy capturing
)     end the group
\$\$  two more dollar signs

The capture group is then used in the string '<i>$1</i>'. $1 refers to the group that the regex has captured.

然后在字符串' $ 1 '中使用捕获组。 1美元是指正则表达式捕获的组。

#2


9  

Use this:

用这个:

str.replace(/\${2}(.*?)\${2}/g, "<I>$1</I>");
\${2} matches two $ characters
(.*?) matches your string to be wrapped
\${2} same as above
/g matches globally

jsFiddle

的jsfiddle

If you wanted something in jQuery:

如果你想要jQuery中的东西:

$("#txt").val().replace(/\${2}(.*?)\${2}/g, "<I>$1</I>");

Markup:

标记:

<textarea id="txt">I'm $$Zach$$</textarea>

jsFiddle

的jsfiddle

Wrap it in a function for best use:

将其包装在一个功能中以便最佳使用:

var italics = function (str) {
    return str.replace(/\$\$(.*?)\$\$/g, "<I>$1</I>");
}

italics($("#txt").val());

Seems like you want to make a syntax similar to Markdown. Why not just use a Markdown parser for your fields instead of reinventing the wheel?

好像你想要制作类似于Markdown的语法。为什么不在你的领域使用Markdown解析器而不是重新发明*呢?

Showdown JS is actively developed and you get the same Markdown syntax as with any other markdown syntax.

Showdown JS正在积极开发中,您可以获得与任何其他markdown语法相同的Markdown语法。

#3


3  

Using the string .replace method will do.

使用字符串.replace方法可以。

.replace(/\$\$(.*?)\$\$/g, '<I>$1</I>')

#4


2  

Use this, change link and tag for extended linkify function :

使用此选项,更改链接和标记以扩展linkify功能:

String.prototype.linkify = function() {
    var wikipediaPattern = /<wikipedia>(.+?)<\/wikipedia>/g; 
    return this.replace(wikipediaPattern, '<a href="http://fr.wikipedia.org/wiki/$1">$1</a>');
}