I have a string that can be a ma separated list of \w
, such as:
- abc123
- abc123,def456,ghi789
I am trying to find a JavaScript regexp that will return ['abc123']
(first case) or ['abc123', 'def456', 'ghi789']
(without the ma).
I tried:
^(\w+,?)+$
-- Nope, as only the last repeating pattern will be matched, 789^(?:(\w+),?)+$
-- Same story. I am using non-capturing bracket. However, the capturing just doesn't seem to happen for the repeated word
Is what I am trying to do even possible with regexp? I tried pretty much every bination of grouping, using capturing and non-capturing brackets, and still not managed to get this happening...
I have a string that can be a ma separated list of \w
, such as:
- abc123
- abc123,def456,ghi789
I am trying to find a JavaScript regexp that will return ['abc123']
(first case) or ['abc123', 'def456', 'ghi789']
(without the ma).
I tried:
^(\w+,?)+$
-- Nope, as only the last repeating pattern will be matched, 789^(?:(\w+),?)+$
-- Same story. I am using non-capturing bracket. However, the capturing just doesn't seem to happen for the repeated word
Is what I am trying to do even possible with regexp? I tried pretty much every bination of grouping, using capturing and non-capturing brackets, and still not managed to get this happening...
Share Improve this question edited Sep 19, 2014 at 16:47 user663031 asked Sep 19, 2014 at 2:21 MercMerc 17.1k18 gold badges84 silver badges131 bronze badges 5-
What's wrong with
string.split(',')
? – elclanrs Commented Sep 19, 2014 at 2:22 - 1 It will split according to ',', whereas I want to check that the matches ply with \w – Merc Commented Sep 19, 2014 at 2:38
-
What is the expected output for
=badstuff,goodstuff
? Do you have to deal with escaping mechanism in ma separated values? – nhahtdh Commented Sep 19, 2014 at 3:44 - Nope, no escaping necessary... just a 'no' in case it doesn't satisfy \w – Merc Commented Sep 19, 2014 at 4:03
- So do you discard the whole input if anything is invalid? Or do you want to keep anything? – nhahtdh Commented Sep 19, 2014 at 5:04
6 Answers
Reset to default 6If you want to discard the whole input when there is something wrong, the simplest way is to validate, then split:
if (/^\w+(,\w+)*$/.test(input)) {
var values = input.split(',');
// Process the values here
}
If you want to allow empty value, change \w+
to \w*
.
Trying to match and validate at the same time with single regex requires emulation of \G
feature, which assert the position of the last match. Why is \G
required? Since it prevents the engine from retrying the match at the next position and bypass your validation. Remember than ECMA Script regex doesn't have look-behind, so you can't differentiate between the position of an invalid character and the character(s) after it:
something,=bad,orisit,cor&rupt
^^ ^^
When you can't differentiate between the 2 positions, you can't rely on the engine to do a match-all operation alone. While it is possible to use a while loop with RegExp.exec
and assert the position of last match yourself, why would you do so when there is a cleaner option?
If you want to savage whatever available, torazaburo's answer is a viable option.
Live demo
Try this regex :
'/([^,]+)/'
Alternatively, strings in javascript have a split method that can split a string based on a delimeter:
s.split(',')
Split on the ma first, then filter out results that do not match:
str.split(',').filter(function(s) { return /^\w+$/.test(s); })
This regex pattern separates numerical value in new line which contains special character such as .
,,
,#
and so on.
var val = [1234,1213.1212, 1.3, 1.4]
var re = /[0-9]*[0-9]/gi;
var str = "abc123,def456, asda12, 1a2ass, yy8,ghi789";
var re = /[a-z]{3}\d{3}/g;
var list = str.match(re);
document.write("<BR> list.length: " + list.length);
for(var i=0; i < list.length; i++) {
document.write("<BR>list(" + i + "): " + list[i]);
}
This will get only "abc123" code style in the list and nothing else.
May be you can use split function
var st = "abc123,def456,ghi789";
var res = st.split(',');