1

How to use Regex match() to split numbers and string along with special character in jQuery?

For example:

str = "+10días" // this is my the string. (number + string along with any special character)

string = str.match("needed stuf");

I need like this so that string[0] = 10 and string[1] = "días". How can I do that?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

2 Answers2

0

Try this code:

var str = "+10días"

var parts= str.match(/[^0-9]+|[0-9]+/g)
Vikram Biwal
  • 2,618
  • 1
  • 25
  • 36
  • hi crazy, thanks for your answer, but the special character in my string is not only "í" it contain any special character . can you please suggest it works for any special character as per my requirment. – Suriya Ravikumar Nov 20 '17 at 11:24
0

<html>
<head>
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            var str = "+10días"
            var arr = new Array();
            str = str.replace(str.match(/[^\w\*]/), '')
            arr.push(str.match(/^[0-9]+/g)[0])
            str = str.replace(str.match(/^[0-9]+/g)[0], '')
            arr.push(str)
            console.log(arr)
        });
    </script>

</head>
</html>
Eshu
  • 185
  • 6