1

I would like to extract full name from the string using regular expression. How can i do that? This code gives me empty value of result. What's wrong?

var p = '№ 46/20 John Smith Newmore 23.01.2020';
var result = p.match(/^([a-zA-Z0-9]+|[a-zA-Z0-9]+\s{1}[a-zA-Z0-9]{1,}|[a-zA-Z0-9]+\s{1}[a-zA-Z0-9]{3,}\s{1}[a-zA-Z0-9]{1,})$/);

My expected result matches the regular expression:

Existing data - string: '№ 46/20 John Smith Newmore 23.01.2020'

Expected result: 'John Smith Newmore'

7
  • Use p.replace(/[^a-zA-Z ]+/g,'').trim() Commented Mar 5, 2020 at 7:14
  • @WiktorStribiżew how can i use cyrillic symbols? Commented Mar 5, 2020 at 10:12
  • p.replace(/[^а-яА-ЯёЁa-zA-Z ]+/g,'').trim() if you ask for Russian ones. Or p.replace(/[^\u0400-\u04FFa-zA-Z ]+/g,'').trim() to handle any Cyrillic chars. Commented Mar 5, 2020 at 10:19
  • @WiktorStribiżew i got the same "Uncaught SyntaxError: Invalid regular expression: /[^Р°-СЏРђ-ЯёЁa-zA-Z ]+/: Range out of order in character class" Commented Mar 5, 2020 at 10:21
  • That's a problem with your file encoding. But p.replace(/[^\u0400-\u04FFa-zA-Z ]+/g,'').trim() will work with all Cyrillic chars. Commented Mar 5, 2020 at 10:23

2 Answers 2

1

var str = '№ 46/20 John Smith Newmore 23.01.2020';

console.log(str.replace(/[^a-zA-Z ]/g, ""));

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

Comments

0

You can try matching anything between number followed by space and space followed by number using RegEx lookbehind and lookahead:

var p = '№ 46/20 John Smith Newmore 23.01.2020';
var result = p.match(/(?<=\d+ ).+(?= \d+)/)[0];
console.log(result);

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.