+ 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
7 Antworten
+ 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(/[.,@;]/);
+ 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"]
+ 2
Oh thank you.
+ 2
And if i want more characters? ex.12,34.56;78
+ 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
+ 2
Ok, thanks going to learn this properly soon
+ 2
So thanks for well explaining, now I can proceed further with my little project.