Question

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.

Was it helpful?

Solution

Try this on the sqlString variable

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

subString should contain your required string.

OTHER TIPS

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]); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top