0

I have the following text inside a javascript string variable:

here is some text Page.Title = "info with åäö"; here is more text

I need to target the following and get it placed in a javascript variable (including the quotes):

Page.Title = "info with åäö";

so I can manipulate that into this:

Page.Title = " some other info with 123";

and replace and put back into the string so it looks like this:

here is som text Page.Title = "some other info like 123"; here is more text

The "Page.Title =" are always the same as well as the last semicolon, but the string between the quotes, like: "info with åäö"; can vary

So how can I best target that and change that string with another string?

This is what I tried:

strFind = /Page.Title = [a-z]+/;
strHTML_value = here is some text Page.Title = "info with åäö"; here is more text
strToBeReplace = strHTML_value.match(strFind)[1]
alert(' strToBeReplace ' + strToBeReplace); // = Page.Title = "info with åäö";
strNewValue = Page.Title = "some other info like 123";
strHTML_value = strHTML_value.replace(strToBeReplace, strNewValue);
2
  • 1
    Two lines of your code are not even syntactically correct... Commented Mar 9, 2012 at 9:21
  • 1
    could there be backslash-escaped quotes and backslash-escaped backslashes inside the quotes? Will the quoted text ever use single quotes instead of double? Commented Mar 9, 2012 at 9:22

2 Answers 2

1

You might want to look at some Regular Expression in JavaScript context: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

First of all your code has some syntax errors, but besides that, your regex will not do what you want it to.

var titleRegex = /Page\.Title\s=\s(".+")/;
var stringValue = "here is some text Page.Title = \"info with åäö\"; here is more text";
var replaceValue = "A New Title";
var newTitle = stringValue.replace(/(Page\.Title\s=\s")([^"]+)(")/, '$1' + replaceValue + '$3');
0

Try the following regex/code:

var str = 'here is some text Page.Title = "info with åäö"; here is more text';
var replaceWith = 'some other info like 123';
str = str.replace(/(.*Page\.Title = ")([^"]+)(".*)/gi, '$1' + replaceWith + '$3');

Note that the "[^"]+" sequence finds an opening quote, followed by one or more non-quote characters, followed by a closing quote.

3
  • Ah, too late with my answer, though my code matches a smaller amount of text. Commented Mar 9, 2012 at 9:41
  • It must be great having such a knowledge... :-) Thank you very much. It was exactly what I was looking for. You guys are true heroes! Commented Mar 9, 2012 at 9:57
  • @lars I won't lie. It is pretty awesome. :-) But it's easily obtained by constantly self-educating on sites like StackOverflow and others. If you constantly seek to get better and learn more, you will! Commented Mar 9, 2012 at 9:59

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.