1

An user was kind enough to help me out with my regex in PHP, but now I'm realizing that I need it in JS too. I've been spending a crazy amount of time testing/experimenting, but without success.

In JS, I would like to turn this :

[data-user="12345-6789" data-userId="3456-789"]John Smith[/] ...Blablabla some other text... [data-user="4567-891011" data-userId="5678-9101112"]Foo Bar[/]

Into this :

<span data-user="12345-6789" data-userId="3456-789">John Smith</span> ...Blablabla some other text... <span data-user="4567-891011" data-userId="5678-9101112">Foo Bar</span>

Here is my PHP regex, which works perfectly :

$regex = '~\[(data-user="[\d-]+")\s+(data-userId="[\d-]+")\]\s*(.+?)\s*\[\/\]\s*(.*)~is';

while (preg_match($regex, $string)) {
    $string = preg_replace($regex, "&lt;span $1 $2&gt;$3&lt;/span&gt;$4", trim(strip_tags($string)));
}

P.S. I'm fully aware that parsing HTML with a regex is frowned upon, but since everything is consistent, I thought this would be the best approach.

Thank you very much.

4
  • 2
    So what's your question? Commented Oct 27, 2015 at 19:52
  • 2
    What Javascript have you tried? Commented Oct 27, 2015 at 19:53
  • 1
    @Kyll Looks like it's just "How to I make this code do the same thing in JavaScript". Commented Oct 27, 2015 at 19:53
  • Ah. Then, Stack Overflow is a Question and Answer site, not a code translation service. Try to translate the code yourself first, then come to us when you are stuck, making sure to show us what you have tried. Commented Oct 27, 2015 at 19:54

2 Answers 2

2

I simplified the RegEx and made it less hard-coded, allowing you to have other attributes and such inside the tag as well.

In JavaScript you can just use String.prototype.replace() as it accepts RegEx.

var string = '[data-user="12345-6789" data-userId="3456-789"]John Smith[/] ...Blablabla some other text... [data-user="4567-891011" data-userId="5678-9101112"]Foo Bar[/]'

var regex = /\[([^\]]*)\]\s*([^\[]*)\s*\[\/\]/ig;

console.log(string.replace(regex, '<span $1>$2</span>'));

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

1 Comment

Thank you very much. I went with @Zsw's answer as it allowed me to keep things a little more strict, but I might take your approach if I ever need a little more loose. Thank you.
1

It looks to me like translating it verbatim works just fine. Just have to mind the changes in the regex itself, namely, changing the ~ to / and removing the s modifier.

var regex = /\[(data-user="[\d-]+")\s+(data-userId="[\d-]+")\]\s*(.+?)\s*\[\/\]\s*(.*)/i;
var str = '[data-user="12345-6789" data-userId="3456-789"]John Smith[/] ...Blablabla some other text... [data-user="4567-891011" data-userId="5678-9101112"]Foo Bar[/]';
var html = str;

while (regex.test(html)) {
    html = html.replace(regex, '&lt;span $1 $2&gt;$3&lt;/span&gt;$4');
}
console.log(html);

4 Comments

Thank you so much. This shows me how close I was. I did not remove the 's' modifier. Thanks again.
You cannot use the /smodifier in JavaScript. Replace all dots with [\s\S] instead.
@stribizhev Understood. Thank you.
Also, preg_replaceperforms a global search and replace (in JS, you need /g). However, I doubt you need that global search here.

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.