What is the best way to split a String in jQuery from the first found number.
Example: Speelhof(STE) 0 /W301
I want to split the string in 2 parts (the split must happen on the first found number)
Result should be 2 parts:
Speelhof(STE) and 0 /W301
The strings can differ with different numbers, so I would need a way to substring from any number…
Can anyone help me out? Thanks!
This problem is a perfect candidate for regular expressions:
You can then access the two components as elements of the returned array:
match[1]andmatch[2].There’s no need to use jQuery here.
To explain how the pattern works:
^denotes the beginning of the string;(.+)denotes one or more (+) of any character (.), and the parentheses capture this group and allow you to access it in the array that is returned. The second group comprises a digit,\d, followed by one or more of any character,.+, like in the first group.$denotes the end of the string.