Javascript split by spaces, but not within html-tags

Erlend Jones

My first goal is to split a string by spaces, but not the ones within html-tags.

I've tried to rewrite the following, unsuccessfully: Javascript split by spaces but not those in quotes

What would the regex look like in: arr = fullHtmlString.split(?); ?


My main goal is to shift an IMG-tag by one space at a time. After that I'll iterate over the array, search for the img-tag, remove it, and add it the next item, and finally join the array.

The code I use at the moment is quite comprehensive and use jQuery extensively to achive the goal.

Input:

<div>
    <p><img class=something>Some text.</p>
    <p>Some more text.</p>
</div>

Deisred output first time:

<div>
    <p>Some<img class=something> text.</p>
    <p>Some more text.</p>
</div>

...second time:

<div>
    <p>Some text.<img class=something></p>
    <p>Some more text.</p>
</div>

...third time:

<div>
    <p>Some text.</p>
    <p><img class=something>Some more text.</p>
</div>
Patrick Evans

You should not try to do this with a regular expression, why explained here.

You can use DOM properties and methods though

function run(){
  var img  = document.querySelector(".something"),
   sibling = img,
   parent  = img.parentNode,
   next    = parent.nextElementSibling;

  //Search for the next textNode
  while((sibling = sibling.nextSibling) && sibling.nodeType !=3);

  if(sibling) {
    //split the text only once, 
    //so "some more text" becomes ["some","more text"]
    var textParts = sibling.textContent.split(/ (.*)?/,2);

    //put the first split item before the sibling
    parent.insertBefore(document.createTextNode(textParts[0]+" "),sibling);

    //replace the sibling with the img element
    parent.replaceChild(img,sibling);

    //finally if there was more text insert it after the img
    textParts[1] && parent.insertBefore(document.createTextNode(textParts[1]),img.nextSibling);    
  } else if(!sibling && next) {
    //no sibling in the current parent, 
    //so prepend it to the next available element in parent
    next.insertBefore(img,next.firstChild);
  } else {
    clearInterval(timer);
  }
}

var timer = setInterval(run,2000);
<div>
    <p><img class="something" src="http://placehold.it/10x10">Some text.</p>
    <p>Some <span>skip me</span> more text.</p>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript split by spaces, but not within html-tags

From Dev

How to split HTML text ignoring spaces in tags

From Dev

How to split HTML text ignoring spaces in tags

From Dev

Split PHP and HTML tags and marge it together in JavaScript

From Dev

javascript remove html tags but not content within them and not <a > tags with regex

From Dev

javascript - replace all white spaces with html tags to enclose remaining words

From Dev

Regex within html tags

From Dev

Regex within html tags

From Dev

split html by tags

From Dev

Javascript RegEx replace all characters not within HTML tags

From Dev

Change color of specific text within HTML tags using Javascript

From Dev

Unable to access Html tags present within xsl in JavaScript

From Java

Split string by spaces, but not spaces within newlines, and preserve newlines

From Dev

Javascript split by spaces but not those in quotes

From Dev

Split array rows with spaces in Javascript

From Dev

HTML tags appearing as character spaces in HTML textfield

From Dev

HTML tags appearing as character spaces in HTML textfield

From Dev

Regex match within html tags

From Dev

Validating php within html tags

From Dev

How to split html tags in jquery?

From Dev

Split string ignoring html tags

From Dev

Split all html tags into a array

From Dev

Remove white spaces between tags in HTML

From Dev

Replace all spaces except the ones with in HTML tags

From Dev

Remove spaces between tags in an html file in vim

From Dev

I want to remove spaces between tags in HTML

From Dev

Regex to replace first and last spaces in html tags

From Dev

javascript : focusOffset with html tags

From Dev

Javascript substring with html tags

Related Related

HotTag

Archive