Javascript. Reverse order of words in string, but not special characters Hello I am hoping someone can provide me feedback on my solution to this prob. If there are additional edge cases I should consider etc. Thank you!
The question: &quot;Given a sentence with a set of delimiters, reverse the order of the words in the sentence and keep the delimiters in place.&quot;
let isAlpha = (char) =&gt; {
  let pat = /[a-zA-z]/igm;     // match any alphabetic character
  if(char.match(pat)){
      return true
  } else {
      return false
  }
}

function reverse(s){
  
  let specialArr = []   

  for(ltr of s){ 
    if(!isAlpha(ltr)){                        // pull out all special characters in order
      specialArr.push(ltr)
    } 
  }
  
  let regCharArr = s.split(/[^a-z]/ig).reverse()     // use split to pull out all words separated by non alpha characters. then reverse
  
  let result = []                                    // init result array
  
  if(isAlpha(s[0])){
    result = regCharArr.flatMap((val, idx) =&gt; [val, specialArr[idx]])   //use flatmap to combine the two arrays...if origin array starts with letter, combine special array into reg array
  }
  
  if(!isAlpha(s[0])){
    result = specialArr.flatMap((val, idx) =&gt; [val, regCharArr[idx]])    // vice versa
  }

  for(let i = 0; i &lt; result.length; i++){         // remove any undefineds
    if(result[i] === undefined){
      result.splice(i, 1)
    }
  }
  return result.join('')
}


// let sentence = &quot;one|twotwo/three:four&quot;  // four|three/twotwo:one
let sentence = &quot;|brian/steve:chris|jon&quot;     // |jon/chris:steve|brian

console.log(reverse(sentence))

```
The current implementation of isAlpha ...
let isAlpha = (char) =&gt; {
  // match any alphabetic character.
  let pat = /[a-zA-z]/igm;

  if(char.match(pat)){
    return true;
  } else {
    return false;
  }
};

... can be refactored into something as short as this ...
function isAlpha(char) {
  return (/[a-zA-z]/).test(char);
};

... or maybe even that ...
function isAlpha(char) {
  // \w matches any word character
  // and is equal to [a-zA-Z0-9_]
  return (/\w/).test(char);
};

Why is that?
isAlpha gets invoked from within reverse always by providing a single character. Thus the regex can be simplified from /[a-zA-z]/igm to /[a-zA-z]/. Since isAlpha is supposed to return a boolean value do not use match and do no not return either true or false separately but use RegExp.prototype.test instead which already provides the correct return value.
Thats all so far but I will continue looking through the code, since as of now I don't understand why one wants to test a string stepwise character by character.
There is an answer of mine, regarding your question at StackOverflow. Maybe you get some inspiration from an alternative approach.