জাভাস্ক্রিপ্ট স্ট্রিং অনুসন্ধান (JavaScript String Search Tutorial in Bangla)

 



search() মেথড একটি প্যারামিটার সমর্থন করে। রেগুলার এক্সপ্রেশন। যেকোন স্ট্রিংয়ে রেগুলার এক্সপ্রেশন প্যারামিটার ব্যবহার search(regex) মেথড দিয়ে সব সাবস্ট্রিং/শব্দগুলি কোন পজিশনে আছে সেটা দেখতে পারেন।

রেগুলার এক্সপ্রেশনের বদলে যদি কোন স্ট্রিং দেন তাহলে এই মেথড ভিতরে ভিতরে RegExp() নামে একটা ফাংশন/কনস্ট্রাক্টর ব্যবহার করে সেটাকে রেগুলার একসপ্রেশনে রুপান্তর করে তারপর খোজা শুরু করবে।

রেগুলার এক্সপ্রেশন সম্পর্কে ধারনা পেতে পিএইচপি রেগুলার এক্সপ্রেশন একটি টিউটোরিয়াল আছে সেটা দেখতে পারেন।

search(regex) মেথড স্ট্রিংয়ের প্রথম থেকে খোজা শুরু করবে কোথায় মিল পাওয়া যায় (রেগুলার এক্সপ্রেশনের সাথে), যখনি প্রথম মিল পেয়ে যাবে তখন সেই সাবস্ট্রিং/শব্দের পজিশন রিটার্ন করে যদি মিল না পায় তাহলে -1 রিটার্ন করবে। যেমন

01.var regExp = /codestrickz /;
02.var sen = "What do you know about codestrickz. codestrickz is the largest Bengali tutorial site.";
03.var isMatch = sen.search(regExp);
04. 
05.if(isMatch  != -1){
06.document.write("There was a match at position " + isMatch);
07.}else{
08.document.write("There was no match in the first string");
09.}


প্রয়োগ দেখুন


** এখানে দেখুন codestrickz এটা দুবার আছে স্ট্রিংয়ে, কিন্তু প্রথমটির পজিশন নিয়েছে। তাই আউটপুটে ২৩ নম্বর পজিশন দেখাচ্ছে।

এখানে regExp এর জায়গায় যেকোন রেগুলার এক্সপ্রেশন ব্যবহার করতে পারেন।


searh() এর মত আরকেটি মেথড আছে যার নাম match() এটা দিয়ে মিলে যাওয়া স্ট্রিং/শব্দগুলি বের করে আনা যায়। যেমন

1.var regExp = /codestrickz/g;
2.var sen = "What do you know about CodesTrickz. CodesTrickz is the largest Bengali tutorial site.";
3.var isMatch = sen.match(regExp);
4. 
5.document.write("Strings those matches are : " + isMatch);
6.// and the array becomes isMatch = ['codestrickz', 'codestrickz'];

আউটপুট

Strings those matches are : CodesTrickz,CodesTrickz


যদি অ্যারের ইনডেক্স ধরে দেখতে চান তাহলে isMatch[0] এভাবে দিয়ে দেখতে পারেন।

Post a Comment

Cookie Consent
Little Blog serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the data/wifi internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.