skip to Main Content

I have a string Automation Donot Delete - Automation 1&nbsp;<i class="arrow"></i>&nbsp;GoogleContacts - Automation that is extracted through selenium innerHtml.

I want to split the above string separated by &nbsp;<i class="arrow"></i>&nbsp;.

I did tried to replace the nbsp with additional space and split the string but it is splitting the string into three parts rather two as there is an additional space between Automation Donot Delete"

I did tried the below scripts but nothing resolved my issue.

var input="Automation Donot Delete - Automation 1&nbsp;<i clas =icon></i>&nbsp;GoogleContacts - Automation
input1=input.replace("<[^>]+>","")
input2=input1.replace("&nbsp;"," ")
var arr=input2.split("s{2,}","")

I am expecting an array as ['Automation Donot Delete - Automation 1' , 'GoogleContacts - Automation'] but it is giving me ['Automation', 'Donot Delete - Automation 1', 'GoogleContacts - Automation']

2

Answers


  1. What about this?

    https://jsfiddle.net/0xey2394

    function splitString() {
      var input = document.getElementById("inputString").value;
      input=input.replace("&nbsp;"," ")
      var arr = input.split(/<i class="arrow"></i>/);
      document.getElementById("output1").innerHTML = arr[0].trim();
      document.getElementById("output2").innerHTML = arr[1].trim();
    }
    
    Login or Signup to reply.
  2. I do not understand why you are trying to replace parts before splitting?

    You can simply split the string, as you describe by using split:

    let input = 'Automation  Donot Delete - Automation 1&nbsp;<i class="arrow"></i>&nbsp;GoogleContacts - Automation';
    
    const inputSplit = input.split('&nbsp;<i class="arrow"></i>&nbsp;')
    
    console.log(inputSplit);

    If you want to be more fancy, and also going by your questions title, maybe you want to split on both &nbsp; and <>. Then you need some RegEx.

    Doing simply input.split(/(&nbsp;|<[^>]+>)/) would give us an array of all parts, eg: ["Automation Donot Delete - Automation 1", "&nbsp;", "", "<i class="arrow">", "", "</i>", "", "&nbsp;", "GoogleContacts - Automation"]

    This is a bit annoying, so we could update our regex to do a non-capturing group; input.split(/(?:&nbsp;|<[^>]+>)/) and this would give us same array but any instance of &nbsp; and <> would be empty strings, eg. ["Automation Donot Delete - Automation 1", "", "", "", "GoogleContacts - Automation"]

    To fix that we can filter away any empty strings with input.filter((i) => i !== '').

    All of this together:

    let input = 'Automation  Donot Delete - Automation 1&nbsp;<i class="arrow"></i>&nbsp;GoogleContacts - Automation';
    
    const inputSplit = input.split(/(?:&nbsp;|<[^>]+>)/).filter((i) => i !== '');
    
    console.log(inputSplit);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search