নীচে জাভাস্ক্রিপ্টের বিকল্পের পিছনে একটি ইতিবাচক চেহারা রয়েছে যা দেখিয়েছে কীভাবে 'মাইকেল' সহ লোকের প্রথম নামটি তাদের প্রথম নাম হিসাবে ক্যাপচার করতে হয়।
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) ));