Regular Expressions (RegEx) এবং replace() মেথড ব্যবহার করে JavaScript-এ স্ট্রিংয়ের ভিতর যেকোনো ক্যারেক্টার রিপ্লেস করার পদ্ধতি শিখুন।
মান লইয়া আপনার একটি টেক্সট ব্লক আছে এবং আপনি মেন করা ছিলেন ইম ড্যাশ (—) ব্যবহার করতে, কিন্তু ভুলবশত আপনি হাইফেন (-) ব্যবহার করেছেন:
const textBlock = "When you’re typing fast it’s normal to make a few spelling mistakes here and there - it just means you’re human." সৌভাগ্যবশত, আমরা JavaScript-এ একটি রেগুলার এক্সপ্রেশন এবং replace() মেথড ব্যবহার করে এটিকে ঠিক করতে পারি:
const textBlockCorrected = textBlock.replace(/-/g, "—") console.log(textBlockCorrected) // "When you’re typing fast it’s normal to make a few spelling mistakes here and there — it just means you’re human." কি একই লাগছে? আমাকে বিশ্বাস করুন, এটি নয়। নজদিক থেকে দেখুন। অথবা textBlock এবং textBlockCorrected এর রেজাল্ট প্রিন্ট আউট করুন যা পার্থক্য দেখতে সহজ করবে:
console.log(textBlock) //"When you’re typing fast it’s normal to make a few spelling mistakes here and there - it just means you’re human." console.log(textBlockCorrected) // "When you’re typing fast it’s normal to make a few spelling mistakes here and there — it just means you’re human." নোট: টাইপোগ্রাফিতে, লেটার এবং সিম্বলের মধ্যে পার্থক্য বোঝা অনেক সহজ হয় যখন ফন্টটি মোনোটাইপ নয় যেমন আমরা কোড উদাহরণের জন্য ব্যবহার করি (ফন্ট নাম Menlo)। কিন্তু আমি em, en, এবং হাইফেনের ডিটেইলস টাইপোগ্রাফি-ওরিয়েন্টেড টিউটোরিয়ালের জন্য রাখছি।