samiles samiles - 1 year ago 35
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=[];
if(list[i].indexOf('#') == 0){

//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:


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
cd ^x y !a


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 =[!#^@]/);
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 =[!#^@]/);
  if (specialCharIndex != -1) {
    text = modifiedString.substring(0, specialCharIndex);
    text = text.trim();
    i += specialCharIndex + 1;
  } else {
    text = modifiedString.substring(0);
    text = text.trim();
    i = inputString.length;