-2

I am trying to split the following string:

s = "Whyy noot this thing? Noot!"

into an array using the following delimiters:

delimiters = [
    ' ',
    '?',
    '!'
];

I believe that I can use this type of expression:

var array = string.split();

and that I can use RegEx.

But I don't know what to write between the parentheses in split().

user1283776
  • 19,640
  • 49
  • 136
  • 276
  • As the documentation for `split` says, you put a regexp which matches whatever you want to use as a delimiter. If you want to use two things as a delimiter, in other words to have regexp which matches any of two things, then as the documentation for regexp says, you use the alternation operator (`|`), or a character class (`[]`). Where were you having a problem? –  Mar 09 '16 at 15:59

1 Answers1

0

You can use a RegExp to achieve that.

var str = "Whyy noot this thing? Noot!";
var arr = str.split(/[\s\?!]+/);
jherax
  • 5,238
  • 5
  • 38
  • 50
  • 1
    No need to escape the `?`. –  Mar 09 '16 at 15:57
  • According to http://regexr.com/ is not necessary. It is a rule of mine always escape special characters in RegExp. – jherax Mar 09 '16 at 15:59
  • Would you mind to modify your answer so that /[\s\?!]+/ is set in the variable delimiters with comments showing which characters represent ?, ! and space, and then pass that into the split method. – user1283776 Mar 09 '16 at 16:07
  • The answer have the reference to MDN documentation for [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Special_characters_meaning_in_regular_expressions), you can dig into the page. – jherax Mar 09 '16 at 20:27