0

I'm trying to get the content of a string within a string, delimited by double quotation marks ("). My HTML is as following:

<body>
  <div>
    sqlString = "UPDATE galleria SET image_description = @image_description WHERE id = " + image.Id;
  </div>
</body>

and I'm trying to get the

UPDATE galleria SET image_description = @image_description WHERE id = 

as a single string via my javascript. I know this is probably possible using indexOf or some related method, but I can't seem to get it to work.

2 Answers 2

1

Try this on the sqlString variable

var lastIndex = sqlString.lastIndexOf("=");
var subString = sqlString.substring(1, lastIndex);

subString should contain your required string.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah. That would work. Do you have any idea if I want to concatanate the string even further? Like with sqlString = "UPDATE " + dbName + " SET "...
and in such case you want only UPDATE SET as a return string?
I would want UPDATE as one string and SET as another string.
You could extract every second value from the array sqlString.split ('"')
1

Give you DIV tag an id attribute, and reference the div by that id.

<body>
   <div id="div1">
     sqlString = "UPDATE galleria SET image_description = @image_description WHERE id = " + image.Id;
  </div>
</body>

Then match the string using regular expression:

var $div = document.getElementById('div1');
var matches = $div.innerHTML.match(/(?!")([^"]+)/g);

if(matches!==null) {
    alert(matches[1]); 
}

Comments

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.