Reputation: 274
I'm trying to replace something from a css
text value:
"padding-left:5px;"
I want to replace 5px
by another value.
I can't figure out what javascript regex
to use to use in my replace function.
Upvotes: 1
Views: 71
Reputation: 12389
It seems you only want to replace a number, that is preceded by substring padding-left:
str = str.replace(/(padding-left\s*:\s*)[\d.,]+(?=\s*px)/gi, "$1 20");
\s
is a short for whitespace character [ \t\r\n\f]
(?=\s*px)
the lookahead to check if number followed by px
with optional spaceReplace with $1 20
where $1
contains, what's captured inside the first capture group. See fiddle
Upvotes: 1
Reputation: 174696
Just do replace the chars which exists next to :
with the string you want.
string.replace(/:.*/, ":20px;");
or
var s = "padding-left:5px;"
alert(s.replace(/:[^;]*;/, ":20px;"))
Upvotes: 2