0

I need to extract a sub string, particularly "Sweet Heart" Package (SPA02) from this string:

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Find String</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = '<div class="view view-commerce-line-item-table view-id-commerce_line_item_table view-display-id-default view-dom-id-57765ff55f834d6a7ec1b8f510768a90">
<div class="view-content">
<table class="views-table cols-4"><thead><tr><th class="views-field views-field-line-item-title">
            Title          </th>
<th class="views-field views-field-commerce-unit-price">
            Unit price          </th>
<th class="views-field views-field-quantity">
            Quantity          </th>
<th class="views-field views-field-commerce-total views-align-right">
            Total          </th>
</tr></thead><tbody><tr class="odd views-row-first views-row-last"><td class="views-field views-field-line-item-title">
             "Sweet Heart" Package    (SPA02)          </td>
<td class="views-field views-field-commerce-unit-price">
            135.00  CAD          </td>
<td class="views-field views-field-quantity">
            1          </td>
<td class="views-field views-field-commerce-total views-align-right">
            135.00  CAD          </td>
</tr></tbody></table></div>
</div>';
    var n = str.indexOf('class="odd views-row-first views-row-last"><td class="views-field views-field-line-item-title">');
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

I have tried this to find the beginning of the string: str.indexOf('class="odd views-row-first views-row-last"><td class="views-field views-field-line-item-title">'); but it doesn't return a valid integer position, it in fact doesn't return anything... and I'm wondering what I'm doing wrong and how i'd best go about this.

Thanks!

7
  • check you developer console. It looks like you have a syntax error. Specifically, a string across multiple lines. Commented Jan 25, 2015 at 6:12
  • @AlexanderO'Mara I definitely do, are you saying a string across multiple lines is invalid in javascript ? :o Commented Jan 25, 2015 at 6:14
  • Yes, for some reason, that was the design decision. Commented Jan 25, 2015 at 6:16
  • @AlexanderO'Mara do I have any aalternatives, then? Commented Jan 25, 2015 at 6:17
  • 2
    If it comes from a server-side scripting language like PHP, encode it using a JSON encoder. If not, check out this question and this question. Commented Jan 25, 2015 at 6:19

1 Answer 1

1

You can't declare a JavaScript string across multiple lines. You will get a syntax error.

SyntaxError: unterminated string literal

Alternative options include:

Escape the newline:

var str = '<div>\
    test\
</div>';

Concatenate:

var str = '<div>' + 
'    test' + 
'</div>';

Store it in a DOM element:

<script id="my_str" type="text/html">
<div>
    test
</div>
</script>

And retrieve like so:

doucment.getElementById('my_str').innerHTML;

Using PHP:

var str = <?php echo json_encode( $your_html_string ); ?>;
Sign up to request clarification or add additional context in comments.

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.