samiles samiles - 5 months ago 12
Javascript Question

Parse string with JS to pull out words between several special characters

I have a string like:

lorem ipsum !bang #hash #hash2 ^caret word @at sym


I am trying to pull out the words beginning with the various characters - sometimes they can have a space in them, and there can be multiple of each type. So I want to convert this string to a set of values such as:

text: "lorem ipsum"
!: "bang"
#: ["hash", "hash2"]
^: "caret word"
@: "at sym"


It always starts with just the plain string. So far, I've tried a few things, but can't quite get it. For example, I tried splitting the string into an array of words, which kind of works except for spaces:

var list = str.split(' ');

var hashes=[];
$.each(list,function(i,val){
if(list[i].indexOf('#') == 0){
hashes.push(tagslistarr[i]);
}
});

//Result: [hash, hash2]


So this works, but obviously breaks as some can have spaces. It also means duplicating that code for each of the
^ # ! @
.

I also tried:

str.substring(str.lastIndexOf("^")+1,str.lastIndexOf("@"));


Which does return
caret word
with the space correctly, but fails to take account of all the characters. I tried replacing "@" with an array, but no luck.

Any ideas on how I can achieve this?

Many thanks

Edit: Just a note that the various parts can be in any order, as could the amount of them, and they don't all have to be present. So could be
ab !a #b #c #d ^f b a @a
or
cd ^x y !a

Answer

Please have a look at below JS code. Hope it will help you to achieve your goal.

var inputString = "lorem ipsum !bang #hash #hash2 ^caret word @at sym";
var specialChars = "!#^@";
var firstSpecialCharIndex = inputString.search(/[!#^@]/);
var plainText = inputString.substring(0, firstSpecialCharIndex);
var result = {};
result["text"] = plainText;

for (i = firstSpecialCharIndex + 1; i < inputString.length;) {
  var modifiedString = inputString.substring(i);

  var currentChar = inputString.charAt(i - 1);
  if (result[currentChar] == null)
    result[currentChar] = [];
  var text = "";
  var specialCharIndex = modifiedString.search(/[!#^@]/);
  if (specialCharIndex != -1) {
    text = modifiedString.substring(0, specialCharIndex);
    text = text.trim();
    result[currentChar].push(text);
    i += specialCharIndex + 1;
  } else {
    text = modifiedString.substring(0);
    text = text.trim();
    result[currentChar].push(text);
    i = inputString.length;
  }


}

console.log(result);