Skip to content

Isolate part of a string from index to next whitespace?

What’s the best way to do this solve for this next whitespace?

var string = 'hi this is a string with some !things in it"
var index = string.indexOf('!')
var afterbang = string.substring(index, [NEXT SPACE])

result: things


You could do the whole thing with a regular expression:

var afterbang = string.replace(/.*!(S*).*/, "$1");

What that does is match an exclamation point, followed by some amount of non-whitespace (that’s what S means — s with a lower-case “s” matches whitespace, and upper-case “s” is the opposite). The non-whitespace match is parenthesized so that the regex matching process collects that portion of the match. Finally, the second parameter to .replace() indicates that the first matched group should be returned, which will be that collection of non-whitespace characters right after the first exclamation point.

edit — sorry for the error – fixed now. The original code I typed in would leave you with all the rest of the string too, removing only the exclamation point 🙂 I’ve updated it to include leading and trailing .* expressions to make sure we get rid of everything else.

If you need to find all such substrings (that is, all occurrences of “!” followed by some non-whitespace characters) then things get a little more complicated. Personally, I would exploit the fact that JavaScript allows a function to be passed as the second argument of .replace(). When you do that, the matched groups are passed as arguments to the function:

var collection = [];
string.replace(/!(S*)/g, function(_, bangWord) {

After that, the array “collection” will contain all the matches. The suffix “g” on the regular expression causes the match to be done over and over until it’s no longer found in the string, starting each time after the previous match.

(The function has a dummy first parameter, called “_”, that’s just a placeholder. The first parameter to the function is always the entire match from the whole regular expression. The second and subsequent arguments are from the parenthesized groups. Thus, in this case, the first argument would be the string including the exclamation point, and the second argument is just the substring without it.)