Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have a look at the doco for the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String#Methods_unrelated_to_HTML" rel="nofollow">various string methods</a>.</p> <p>You can extract pieces of a string using <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr" rel="nofollow"><code>.substr()</code></a>.</p> <p>You can determine the starting position of a particular substring using <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf" rel="nofollow"><code>.indexOf()</code></a>.</p> <p>You can replace parts of the string that match a pattern using <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace" rel="nofollow"><code>.replace()</code></a>.</p> <p>For your specific example it is not clear what criteria you have used to determine where to insert the string with the body tags, e.g., did you want to insert at the string's halfway point, or after the first closing ">", or after the first space, or find an html opening and closing tag pair with nothing but white-space content, or...?</p> <p>The following would do it after the first closing ">":</p> <pre><code>var str = "&lt;html&gt; &lt;/html&gt;"; var i = str.indexOf("&gt;"); if (i !== -1) str = str.substr(0, i) + "&lt;body&gt; &lt;/body&gt;" + str.substr(i + 1); </code></pre> <p>Note that in most situations you don't want to be using JavaScript to manipulate html markup as a string, much better to use DOM manipulation functions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload