Split string with multiple separator in jQuery

You can split a string with multiple separators in jQuery using a regular expression with the split() method.

In the below example, the regular expression /[ ,;|]+/ matches one or more occurrences of a space, comma, semicolon, or vertical bar. The split() method then splits the string at each occurrence of the regular expression, resulting in an array of substrings.

var str = 'apple,banana;pear|orange';
var arr = str.split(/[ ,;|]+/);

See the Pen Untitled by Nirbhay (@nirbhaysingh) on CodePen.

3 comments :

  1. Replies
    1. Hi Aksa,
      If you want to split a string with multiple separator.
      Suppose your string is "I-love-my,country,India".
      In this string there are two separator dash(-) and comma(,).
      Then you have to write code like this.

      var allword = 'I-love-my,country,India';
      var wordArray = allword .split(/,|-/);

      Above split method I have used regex expression, split method return an array.
      In regex expression it start and end with "/".
      After that enter your each separator with "|".

      Delete
  2. hi nirbhay singh bro how can use dropdwonlist along with grid view using jquery

    ReplyDelete