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?