samiles
samiles

Reputation: 3900

JS Regex to match with a space before special character

I am trying to pull out a series of words starting with special characters from a string. My code is:

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;
    }
}

Now this sort of works, and correctly turns the string:

String start ^a #b #c @d >e *f /g

into:

{"text":"String start ","^":["a"],"#":["b","c"],"@":["d"],">":["e"],"*":["f"],"/":["g"]}

The issue is with using the characters in the original string. Currently, the Regex thinks that string@string should match to @string when it shouldn't. I want to only match when there is a space before a special character.

Is it possible to require a space before using the Regex? Is there any other way to the Regex should be cleaned up, or is it correct for its purpose? Or would I need to write some more JS to check the values then clean them up or something?

Many thanks

Upvotes: 1

Views: 1832

Answers (2)

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

You can use the regex:

/ ([!#^@>\/*-]\w+)/gm

With this code:

var result = {};


var re = / ([!#^@>\/*-]\w+)/gm; 
var str = 'lorem ipsum http://google.com -h ';
var m;

var firstSpecialCharIndex = str.search(/ [!#^@>\/*-]/);
result["text"] = str.substring(0, firstSpecialCharIndex);


while ((m = re.exec(str)) !== null) {
  if (m.index === re.lastIndex) {
    re.lastIndex++;
  }

  var index = m[1].substring(0,1);
  if(result[index] == null)
    result[index] = [];
  result[index].push(m[1].substring(1));
}


console.log(result);

Upvotes: 1

Will Barnwell
Will Barnwell

Reputation: 4089

You can use:

/\s[!#^@>\-/*]/

\s matches all white space characters [\r\n\t\f ]

or if you just want a plain old space, not to include newlines or tabs

/ [!#^@>\-/*]/

Also you shouldn't need to escape all your backslashes that are already escaping things, nor do you need to escape anything except the -

Upvotes: 0

Related Questions