Vytsalo
Vytsalo

Reputation: 748

How to extract substring matching the pattern using JQuery

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'

Upvotes: 1

Views: 341

Answers (2)

user3766508
user3766508

Reputation: 61

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

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

Upvotes: 1

Mamun
Mamun

Reputation: 68943

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);

Upvotes: 0

Related Questions