0

I have a string that contains a lot of

<span style="font-size:8px;">...</span> 
<span style="font-size:14px;"> ...</span>

And i want to replace all font-size values to given value. So i need a regexp to replace all chars beetween "font-size:" and "px;"

P.s. i use Rangy JS library

var iframe = $('iframe')[0];
            var sel =  rangy.getIframeSelection(iframe);
            selected_html=sel.toHtml();
            selected_html.replace("font-size:<some regexp>px;", "36");

Can you suggest a proper regexp?

2
  • Where is the string coming from? I suggest you get rid of the inline styles, add a class and do it in css. Commented Oct 24, 2012 at 7:57
  • 1
    Don't use regex for html. You're using rangy already, why not modify the attributes on the nice DOM nodes it returns instead of regexing html? Commented Oct 24, 2012 at 7:57

1 Answer 1

3
.replace(/(font-size:)\d+(px;)/g, "$1"+36+"$2");
Sign up to request clarification or add additional context in comments.

3 Comments

nice but you should add some explanation!
$1 == (font-size:) && $2 == (px;) nice Answer.
Better allow some spaces. To keep the spaces use: .replace(/(font-size:\s*)\d+(\s*px;)/g, "$1"+36+"$2"); Otherwise: .replace(/(font-size:)\s*\d+\s*(px;)/g, "$1"+36+"$2");

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.