Why m I getting different results for the same regex splitting function?

  javascript, regex, string

I’ve created 2 functions here, and they’re nearly 100% identical. The only difference is that the functions’ regex character sets are ordered differently. Why do these 2 functions result in different outputs?

// FUNCTION 1:
function splitify(str) {
  let divs= str.split(/[." "-,]/)    //order different [inside]
  return divs
}
console.log(splitify("Hello World,I-am code")); 
 //OUTPUT: ["Hello", "World", "I-am", "code"]
//FUNCTION 2
function splitify2(str) {
  let divs = str.split(/[-" ",.]/);   //order different [inside]
  return divs;
}
console.log(splitify2('Hello World,I-am code'));
//OUTPUT: ['Hello', 'World', 'I', 'am', 'code'];

Source: Ask Javascript Questions

LEAVE A COMMENT