javascript - regular expression attribute replace -


I am trying to do some regular expressions which I have worked in one direction but when I change properties

Align what I am trying to achieve, and change this value to the class.

  var s = '& lt; Img src = "" align = "left" class = "smart" heading = "title for reference" /> '; Var reg = New RegExp (/ (?: Align = "(. *?)" *. (?: Class = "(. *)") /); Console.log (s.replace (reg, 'class = "image-$ 1 $ 2"'));   

This fixes and outputs

  & lt; Img src = "" class = "image-left smart" title = "headline for reference" />   

But if I change the class before aligning the html I get the basic HTML string

You can use jQuery to do this.

  var s = '& lt; Img src = "" align = "left" class = "smart" heading = "headline for reference" /> '; $ ('& Lt; div / & gt;'). Add ($ (s) .addClass ('image-' + $ (s) .attr ('align')) .removeAttr ('align')) .html ();   

It has the advantage of not being dependent on special sequence of attributes. And you can definitely add modified HTML to the DOM tree, to get code> $ ('

html text

Comments