What I'm looking for
I have code like this:
<div this-html="text goes here"></div>
After a regex, I want the value of attribute "this-html" be the text between the opening and closing tag, like so:
<div>text goes here</div>
The element can contain other attributes and doesn't have to be a div, it can basically be any other type of element, as long as it uses a closing tag (which doesn't have to be on the same line). It's also possible that the input has text between the tags, like so <div this-html="text goes here">dummy text</div>
, but that can be ignored / should be overwritten with the value of the "this-html" attribute.
What I have
I can't use jQuery or turn the string into a Javascript object, as it may contain PHP (which will then get crippled if you turn it back into a string again). This script is used during a 'publish to html' process of an application, hence it can contain PHP. And so, I'm trying to solve it using regular expressions.
So basically, all I have is Javascript and the HTML I need to work with is just a string, there's no DOM to work with.
Now, I have a regular expression that does this for me, but it doesn't work when you have multiple matches on the same line or when I have another attribute after "this-html".
This is the regex I'm using:
/(<\s*[^<]+?)this-html=['"]{1}(.+)['"]{1}([^>]*>)[\w\W]*?(<\/.+>)/gmi
And I group it back together with $1$3$2$4
.
Now, let's say I have the following input:
<div this-html="text goes here!" class="something">test</div><div this-html="another test">Option is visible on preview/publish</div>
Then my regex pattern will mess this up and I end up with something like this:
<div >text goes here!" class="something">test</div><div this-html="another test</div>
I'm not a regex guru, but I get the feeling this regex could be a whole lot simpler, but I'm stuck here.
Any ideas?