Reputation: 5985
I'm trying to do a monthly installment with some possibilities, like:
and so on... I'm trying to get any character, with multiple spaces OR multiple characters (equals to each other or not). I reach the following code so far... but I could add the exception for numbers, and the result are not always right.
var pattern = /(\#?[a-zA-Z(!0-9) \/]+)/g;
var a = '30/60/90';
var b = a.split(pattern);
$('#yyy').text(b);
$('#xxx').text(b.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="yyy"></label>
<br><br>
<label id="xxx"></label>
I'm HORRIBLE in regex, really "noob" to it, so if could explain in the answer/comment WHY you are doing specific regex, I would be please (so I can learn instead of copy/paste without too much clue)
Upvotes: 0
Views: 614
Reputation: 51330
I'm not so sure I understood your question correctly, but I'll answer as I understood it:
To split on anything except numbers, the solution would be:
var pattern = /\D+/g;
var a = '30/60/90';
var b = a.split(pattern);
$('#yyy').text(JSON.stringify(b));
$('#xxx').text(b.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="yyy"></label>
<br><br>
<label id="xxx"></label>
The regex is pretty simple: \d
means a digit, so \D
means not a digit, so \D+
means a series of characters that are not digits.
It may be even easier if you try matching instead of splitting:
var pattern = /\d+/g;
var a = '30/60/90';
var b = [];
var m;
while (m = pattern.exec(a))
b.push(m[0]);
$('#yyy').text(JSON.stringify(b));
$('#xxx').text(b.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="yyy"></label>
<br><br>
<label id="xxx"></label>
In that case \d+
means a series of digits.
For reference, in JS:
\d
is shorthand for [0-9]
\D
is shorthand for [^0-9]
Upvotes: 2