Split string with multiple separator in jQuery

If you want to split a string with multiple separators then you have to pass regex expression into split method. In below example I have used three character (, : -) as separator.

var numbers = '1,2:3-4';
var numArray = numbers.split(/,|:|-/);

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $('.btnClick').click(function () {
        var numbers = '1,2:3-4';
        var numArray = numbers.split(/,|:|-/);

        for (var i = 0; i < numArray.length; i++) {
          alert(numArray[i]);
        }
      });

    });
  </script>
</head>
<body>
  <div>
    <button class="btnClick">Click</button>
  </div>
</body>
</html>

DISPLAY

6 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
  3. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. Day Trips From Split

    ReplyDelete
  4. I am very thankful to you that you have shared this information with us. I got some different kind of knowledge from your web page, and it is really helpful for everyone. Thanks for share it. niagra falls helicopter ride

    ReplyDelete
  5. Read the terms and situations 카지노사이트 to see the wagering necessities for every. Some casinos offer a small variety of free spins on a particular slot. You can use these to attempt the slot with out risking your individual cash. Free spins usually have an inherent one-time wagering requirement. That means find a way to|you possibly can} normally hold something you win with them.

    ReplyDelete