+ 2

How to split string with more dividing characters?

I need to split (or any other way) get 12 34 56 from ex. 12,34.56. Provided I know the dividing characters. Can someboby help? Thanks

4th Dec 2018, 12:25 PM
Maneren
Maneren - avatar
7 Réponses
+ 3
With regular expression, for character list, instead of using the | (or) operator, it's more practical to use character class (enclosed in square brackets)... in addition, it allow to have less character to be escaped ^^ But be aware of at least two special chars: ^ and -... the first one if is placed at start will be interpreted as negate operator (all chars but not those specified after), and the second one if not at start or at end of the list is interpreted as char range (e.g.: [2-5] is equivalent to [2345]). And even if there are less char that need to be escaped, some remain required, as (obviously) closing square bracket, anti-slash... Example equivalent to the ones provided by Rishi Anand in previous answers: str.split(/[.,]/); str.split(/[.,@;]/);
4th Dec 2018, 8:32 PM
visph
visph - avatar
+ 2
you can use regular expression for this var str = "12,34.56"; var res = str.split(/\.|,/); console.log(res) output will be ["12", "34", "56"]
4th Dec 2018, 12:47 PM
Rishi Anand
Rishi Anand - avatar
+ 2
Oh thank you.
4th Dec 2018, 12:48 PM
Maneren
Maneren - avatar
+ 2
And if i want more characters? ex.12,34.56;78
4th Dec 2018, 12:51 PM
Maneren
Maneren - avatar
+ 2
var str = "12,34.5@46;77" var res = str.split(/\.|,|@|;/); some characters like . have predefined meaning in regular expression that's why I have use \. instead of just . \makes . to behaves as normal character | means or
4th Dec 2018, 1:42 PM
Rishi Anand
Rishi Anand - avatar
+ 2
Ok, thanks going to learn this properly soon
4th Dec 2018, 2:19 PM
Maneren
Maneren - avatar
+ 2
So thanks for well explaining, now I can proceed further with my little project.
4th Dec 2018, 9:46 PM
Maneren
Maneren - avatar