1

Re-writing the question since named capturing groups are not the main issue.

I have the following regex now:

/([a-zA-Z ]*)([0-9]*)/g

The code is working now fine but var m = /([a-zA-Z ]*)([0-9]*)/g.exec('Ashok : 9830011245') is only giving me the Ashok as the result.

m[0]: "Ashok"
m[1]: "Ashok"
m[2]: ""

Sample Strings I need to work it on:

var strings = [
"Ashok : 9812340245",
"Amit Singh :\nChakmir 9013123427\n\nHitendra Singh:\n\nM. : 9612348943",
"ANIL  AGARWAL :  \n09331234728\n09812340442\nMAYANK AGARWAL : \n09123416042",
"JAGDISH SINGH :      098123452187 \n09830111234",
"MD QYAMUDDIN : 09433186333,\n09477215123\nMD TAJUDDIN : \n09831429111\nGYASUDDIN ANSARI :\n08961383686 \nMD BABUDDIN : \n09433336456 \n09903568555\nJAWE",
"Viay Singh : 9330938789,\nBijay Singh : 9330938222",
"Nilu :          09830161000,\n09331863222,\n09830071333,\nSantosh Upadhayay :       09831379555,\n09331727858,\n09830593322"
];

Please guide.

17
  • JS regex engine does not support named capturing groups. Commented Sep 20, 2017 at 8:27
  • does not support named capturing groups yet :p github.com/tc39/proposal-regexp-named-groups Commented Sep 20, 2017 at 8:32
  • @WiktorStribiżew I have removed the name but now my exec function is not returning the numeric group value at all. After Removing my named groups, the new regex is: /([a-zA-Z ]*)([0-9]*)/g. Commented Sep 20, 2017 at 8:41
  • @JaromandaX Please guide. Commented Sep 20, 2017 at 8:42
  • Try ([a-zA-Z]+)\s*([0-9]+). Note you most probably have line breaks in the original string, not 2-char \ns. Note it is a very bad idea to use a regex that matches an empty string with a g modifier (like /([a-zA-Z ]*)([0-9]*)/g) - it might just cause an infinite loop. Commented Sep 20, 2017 at 8:43

1 Answer 1

1

It seems you may extract all the substrings you need with

/^([^:0-9\n]+)\s*(?::\s*)?([0-9]*)/gm

See the regex demo.

Details

  • ^ - start of the line (as m enables the multiline mode)
  • ([^:0-9\n]+) - 1 or more chars other than :, digits and newline
  • \s* - 1 or more whitespaces
  • (?::\s*)? - an optional sequence of : and 0+ whitespaces
  • ([0-9]*) - zero or more digits.

JS demo:

var strings = [
"Ashok : 9812340245",
"Amit Singh :\nChakmir 9013123427\n\nHitendra Singh:\n\nM. : 9612348943",
"ANIL  AGARWAL :  \n09331234728\n09812340442\nMAYANK AGARWAL : \n09123416042",
"JAGDISH SINGH :      098123452187 \n09830111234",
"MD QYAMUDDIN : 09433186333,\n09477215123\nMD TAJUDDIN : \n09831429111\nGYASUDDIN ANSARI :\n08961383686 \nMD BABUDDIN : \n09433336456 \n09903568555\nJAWE",
"Viay Singh : 9330938789,\nBijay Singh : 9330938222",
"Nilu :          09830161000,\n09331863222,\n09830071333,\nSantosh Upadhayay :       09831379555,\n09331727858,\n09830593322"
];

var regex = /^([^:0-9\n]+)\s*(?::\s*)?([0-9]*)/gm;
for (var s of strings) {
  console.log("Looking in: ", s, "\n--------------------------");
	console.log(s.match(regex));
}
// To output groups:
console.log("====Outputting groups====");
for (var s of strings) {
	while(m=regex.exec(s))
    console.log(m[1].trim(), ";", m[2]);
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.