Replace string with regex in javascript -
i have following text inside javascript string variable:
here text page.title = "info åäö"; here more text
i need target following , placed in javascript variable (including quotes):
page.title = "info åäö";
so can manipulate this:
page.title = " other info 123";
and replace , put string looks this:
here som text page.title = "some other info 123"; here more text
the "page.title =" same last semicolon, string between quotes, like: "info åäö"; can vary
so how can best target , change string string?
this tried:
strfind = /page.title = [a-z]+/; strhtml_value = here text page.title = "info åäö"; here more text strtobereplace = strhtml_value.match(strfind)[1] alert(' strtobereplace ' + strtobereplace); // = page.title = "info åäö"; strnewvalue = page.title = "some other info 123"; strhtml_value = strhtml_value.replace(strtobereplace, strnewvalue);
try following regex/code:
var str = 'here text page.title = "info åäö"; here more text'; var replacewith = 'some other info 123'; str = str.replace(/(.*page\.title = ")([^"]+)(".*)/gi, '$1' + replacewith + '$3');
note "[^"]+"
sequence finds opening quote, followed 1 or more non-quote characters, followed closing quote.
Comments
Post a Comment