रेगुलर एक्सप्रेशन्स (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 कहा जाता है), तो अक्षरों और सिम्बल्स के बीच का अंतर बताना बहुत आसान होता है। लेकिन मैं एम, एन, और हाइफ़न्स के बारे में विवरण टाइपोग्राफी ओरिएंटेड ट्यूटोरियल के लिए बचा कर रखूंगा।