Monday 23 April 2018

split string with regular expression in jQuery

split string with number using jquery
Description:
In this example we explain that how to split the string with number using jQuery. Or how to split the string with regular expression using jQuery. Or split string with regular expression in jQuery.

When I was working with one project at that time the client requirement was that if the string contain some number with bracket “)” then it must be split and display in new line.


Like suppose I have sting like “1) test one 2) test two 3) test 4) test 5) test) six"; then client want result like below

1) test one
 2) test two
3) test
 4) test
5) test) six

So to fulfil this demand I was found the one solution that will help you all to split the string with number using regular expression and display string in new line.so below is the JavaScript function that will have used to split string with regular expression.

 Function:

function GetURLParameter(sParam) {
            var sURLVariables = sParam.split(/\d+\)/);
            var sParameterName = "";
            if (sURLVariables.length == 1)
                return sParam;
            else {
                for (var i = 1; i < sURLVariables.length; i++) {

                    sParameterName += i + ")" + sURLVariables[i] + "</br>";

                }
            }

            return sParameterName;
        }

How to Call:

 var newlinesplitstring=GetURLParameter(“1) test one 2) test two 3) test 4) test 5) test) six")

Output is :

1) test one

 2) test two
3) test
 4) test
5) test) six

0 comments:

Post a Comment