नीचे जावास्क्रिप्ट विकल्प दिखाया गया है जिसमें दिखाया गया है कि 'माइकल' वाले लोगों के अंतिम नाम को उनके पहले नाम के रूप में कैसे कैप्चर किया जाए।
1) इस पाठ को देखते हुए:
const exampleText = "Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green Miller and Michael Wood?";
माइकल नाम के लोगों के अंतिम नामों की एक सरणी प्राप्त करें। परिणाम होना चाहिए:["Jordan","Johnson","Green","Wood"]
2) समाधान:
function getMichaelLastName2(text) {
return text
.match(/(?:Michael )([A-Z][a-z]+)/g)
.map(person => person.slice(person.indexOf(' ')+1));
}
// or even
.map(person => person.slice(8)); // since we know the length of "Michael "
3) समाधान की जाँच करें
console.log(JSON.stringify( getMichaelLastName(exampleText) ));
// ["Jordan","Johnson","Green","Wood"]
यहाँ डेमो: http://codepen.io/PiotrBerebecki/pen/GjwRoo
आप नीचे स्निपेट चलाकर भी इसे आज़मा सकते हैं।
const inputText = "Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green Miller and Michael Wood?";
function getMichaelLastName(text) {
return text
.match(/(?:Michael )([A-Z][a-z]+)/g)
.map(person => person.slice(8));
}
console.log(JSON.stringify( getMichaelLastName(inputText) ));