skip to Main Content

I need help with a Regex in JavaScript (for a Photoshop script) to match bold tags around words in a string. (not worried about italic or bolditalic at this time).
I don’t want to split the string at this stage, I just want to chop it up into certain alternating chunks into using match.

// Be <b>bold!</b> Be fabulous! 

Should get match to // (“Be “, “bold!“, “Be fabulous!”) // line commented for obvious reasons

After that, I’ll remove the bold tags – unless Regex can do that in one pass – don’t underestimate it’s power!

This is what I have so far

(.*?)([<b>]+[S]+[</b>]+[s]+)+(.*)/g

Only it doesn’t match everything as seen here

Just for the record, before anyone suggests a much easier JS solution:
In the Photoshop DOM you can’t script regular text mixed with bold. You probably can with Action Manager code, but with generating text that could be a big headache.
To get around this (not an ideal solution) I’ll be using regular text & splitting it up at the appropriate places & swapping to bold.

2

Answers


  1. [<b>] is character class, use simply <b> instead.

    /(.*?)(<b>+S+</b>+s+)+(.*)/g
    

    and change S to [^<]

    /(.*?)(<b>+[^<]+</b>+s+)+(.*)/g
    
    Login or Signup to reply.
  2. You can try:

    <b>(.*?)</b>
    

    Here is online demo

    sample code:

    var re = /<b>(.*?)</b>/gi;
    var str = 'Be <b>bold!</b> Be fabulous! ';
    var subst = '$1';
    
    var result = str.replace(re, subst);
    

    output:

    Be bold! Be fabulous! 
    

    Better try with String.split() function:

    var re = /s*</?b>s*/gi;
    var str = 'Be <b>bold!</b> Be fabulous!';
    
    console.log(str.split(re));
    

    output:

    ["Be", "bold!", "Be fabulous!"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search