Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  7] [ 5]  / answers: 1 / hits: 113988  / 11 Years ago, mon, april 22, 2013, 12:00:00

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:



string: Hey I'm $$zach$$



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



And still keep the rest of the string intact?


More From » jquery

 Answers
74

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.


[#78741] Friday, April 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
calicinthias

Total Points: 447
Total Questions: 101
Total Answers: 118

Location: Botswana
Member since Sat, Dec 31, 2022
1 Year ago
calicinthias questions
Sun, Jan 2, 22, 00:00, 2 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Mon, Aug 10, 20, 00:00, 4 Years ago
;