-2

I have a string in javascript. I want to run some code to process the string. The code runs fine but the function split did not work.

My code:

    // Raw string
    var str = ' first.getage()  person.getinfo( tow.fff(one) , data ) car.getcompany ';
    
        // Trim the space off of th start and end of str
        var str = str.replace(/(^\s+|\s+$)/g, '');
        
        /* This says:
        || Find any literal fragment that is ".get"
        || Then find everything that's a character before ".get"...
        || until there's a space.
        || Now replace that particular space with:
        || a space, / , and another space
        */
        var rgx = /\s\b(?=\w*(?=\.get))/g;
        
        var res = str.replace(rgx, ' \/ ');
         var  splitvalue=res.split(" / ");
           for (var i = 0; i <splitvalue.length ; i++) 
            {
              console.log(i+splitvalue[i]);
            }

Code online: jsbin

The variable splitvalue must be an array containing 3 elements but it only contains 1 element. So what is wrong? Why did the split function not work here?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Sara
  • 45
  • 1
  • 4
  • You can't use `.split` that way - you either have to provide a single character, or a regular expression. You cannot split on a multi-character string like you are trying to do. – Joe May 06 '17 at 12:30
  • What is in res? – stark May 06 '17 at 12:31
  • 1
    I'm confused, it's actually working? looking at your jsbin. the problem is on the alert, have your tried `console.log`? – Roljhon May 06 '17 at 12:32
  • 4
    Joe, that is false information. Multi character strings work fine. – rlemon May 06 '17 at 12:38
  • no the array have only one element I try to alert the length of array and print the length 1 – Sara May 06 '17 at 12:42

2 Answers2

1

Your array does contain 3 elements. You only see one on jsbin because they interrupt your loop after the first iteration. On the browser console I see:

Exiting potential infinite loop at line 19. To disable loop protection: add "// noprotect" to your code
runner-3.41.10.min.js:1:12578
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • no the array have only one element I try to alert the length of array and print the length 1 @melpomene – Sara May 06 '17 at 12:43
  • 1
    @Sara I ran your snippet here on SO and the jsbin version with `// noprotect` added. In both cases I got 3 elements. Show your code that demonstrates the problem with `length` 1. – melpomene May 06 '17 at 12:45
-1

thanks for all people answer my question ... the problem was in the browser of my IDE ... it did not execute some function ... when I try other browser it work fine

Sara
  • 45
  • 1
  • 4