0

This is my sample string:

<p>Whatever #591118</p>

This is my desirable output:

p='<p>Whatever <i class="dn">591118<i></p>';

This is the regex I am trying to write:

r="(.*)#([0-9]+(?=<))(<.*)$";

If I:

alert(p.match(r));

It prints:

<p>Whatever #591118</p>,<p>Whatever ,591118,</p>

I was thinking to use .replace with '$1<i class="dn">$2<i>$3'

But my knowledge of regex and jquery ends here...

Thanks for any help.

4
  • 1
    Is this what you are trying to do? Commented Jul 26, 2014 at 13:48
  • 1
    "I was thinking to use..." did you try it? What was the result? Commented Jul 26, 2014 at 13:53
  • God, it worked! Just change this: var str = '<p>Whatever #591118</p>'; Commented Jul 26, 2014 at 13:55
  • Sorry for the big confusion with the sample string. I edited the question. Commented Jul 26, 2014 at 13:57

2 Answers 2

1

You can use the below code to replace part of the original string based on the regex being matched. The code (I think) is self explanatory.

var regex = /(.*)#([0-9]+(?=<))(<.*)$/; 
var str = '<p>Whatever #591118</p>';
var toReplace = '$1<i class="dn">$2<i>$3'; 

var result = str.replace(regex, toReplace);
console.log(result);

Output: <p>Whatever <i class="dn">591118<i></p>

Demo

RegEx Explanation: (for benefit of future readers)

(.*) - First capturing group ($1) which matches any number of non-newline characters (zero or more). For the above example, this would be equal to <p>Whatever.

([0-9]+(?=<)) - Second capturing group ($2) which is any string of characters starting with one numeric character (that is, 0-9) and a positive look ahead to capture all characters until a < character is encountered. For the above example, this would be equal to 591118.

(<.*) - Third capturing group ($3) which is basically all remaining characters starting with the <. For the above example, this would equal to </p>.

Sign up to request clarification or add additional context in comments.

Comments

1

Regex:

^.*\/\/(\S+)\s+#(\d+)([^,]*).*$

Replacement string:

$1 <i class="dn">$2<i>$3

DEMO

Explanation:

  • ^ Asserts that we are at the start.
  • .*\/\/ It matches all the chars upto the //
  • (\S+) Non-space characters are captured into a group.
  • \s+# One or more spaces followed by # symbol.
  • (\d+) One are more numbers are captured into another group.
  • ([^,]*) Captures any character not of , zero or more times.
  • .* Matches any character zero or more times.
  • $ Asserts that we are at the end.

2 Comments

Hey partner, would you be kind to explain us (miserable mortals) your regex?
^.* begins with 0 or more any chars, \/\/ followed by //, (\S+) followed (catch $1) by 1 or more not white char (space, tab, newline...), \s+ followed by 1 or more white char, (\d+) followed (catch $2) by 1 or more digit , ([^,]*) followed (catch $3) by 0 or more any char but coma, .*$ ends with 0 or more any char. developer.mozilla.org/fr/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.