Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find if a string exist within another string

Status
Not open for further replies.

qwert62

Programmer
Oct 12, 2000
28
0
0
US
I am very new to JavaScript, so please excuse me if this is a basic question.

I need to search a given string for the number of times another string exist within the given string. For example if String 1 = "ABCDEFA", I need to find how many times "A" exist in String 1. I don't care about the position(s) of "A".

Thanks in advance for any suggestions.
 
not a basic question at all.
the first thing and easiest I think that comes to mind is regular expression match method
try this out
var count = 0; //count match variable declaration
str = new String("ABCDEFA") // change to input field if needed
ptrn = new RegExp(/A/g) //regex pattern g for global
strmatches = str.match(ptrn) //populate array with matches
if(strmatches != null) { //check for null
//if there was no match it will be null not 0 length common error
for(var i =0; i < strmatches.length; i++)
{
count = count+1; // loop through and count them
}
alert(count+&quot; matches were found&quot;);
}

and there we have it _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
 
onpnt,

Thanks for the response. It was very helpful. I have it working, but if you don't mind, I have one other question.

Can I use the RegExp to seach for strings that contain special characters. For example I would like to find how many instances of &quot;[[(Start&quot; are in the search string.

Thanks.
 
regular expression special characters can be found by using \ in the pattern
so if you want [ or \ etc.. do \[ or \ _______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
Is your question a
 
here some listings for further explanations and pattern uses
_______________________________________________
[sub]{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }[/sub]
_______________________________________________
for the best results to your questions: FAQ333-2924
Is your question a
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top