473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Search for a number in a string or in an array

What would be the best way to search for a number within a string or an array in javascript? I'm currently using indexof - but if I am searching for 1, would it also return true if the number ends up being 11 or 111?
Feb 19 '08 #1
4 1213
gits
5,390 Expert Mod 4TB
hi ...

you are right ... but you may use a regEx here :

Expand|Select|Wrap|Line Numbers
  1. var s = 'adgf1';
  2. var t = s.match(/1{1}/g);
  3.  
  4. alert(t.length);
that would give you an array of all occurences of 1 in the string s ... but what do you really want to check exactly?

kind regards
Feb 19 '08 #2
I hadn't thought of using regex, but that may work as a great possiblity, thanks :) .

What I'm actually trying to do is look for a number within an array. The number is dynamically set as is the array. Indexof was working correctly, but then I realized my error.

Here is a snippet:
Expand|Select|Wrap|Line Numbers
  1.  var dispCA = new Array(<cfoutput query="getCA">#id#<cfif getCA.currentRow LT getCA.recordCount>,</cfif></cfoutput>);
  2. var dispSup = new Array(<cfoutput query="getSup">#id#<cfif getSup.currentRow LT getSup.recordCount>,</cfif></cfoutput>);
  3. var contID = document.getElementsByName('contactID');
  4.  
Expand|Select|Wrap|Line Numbers
  1. for(var i=0;i<contID.length;i++)
  2. {                         
  3.  if(dispCA.join().indexOf(contID[i].value)>=0)
  4.  {
  5.     contID[i].checked = true;
  6.  }
  7.  else
  8.  {
  9.     contID[i].checked = false;
  10.  }
  11. }
  12.  
Thank you for your help!
Feb 19 '08 #3
gits
5,390 Expert Mod 4TB
hi ...

since you use arrays you may find the following article useful. I wouldn't use indexOf ... just 'real'-compare the id with the array-values ... something like this:

Expand|Select|Wrap|Line Numbers
  1. var a = ['2', '1', 'a'];
  2.  
  3. function arr_contains(arr, val) {
  4.     var ret = false;
  5.  
  6.     for (var i = 0, l = arr.length; i < l; i++) {
  7.         if (arr[i] === val) {
  8.             ret = true;
  9.             break;
  10.         }
  11.     }
  12.  
  13.     return ret;
  14. }
  15.  
  16. // usage
  17. alert(arr_contains(a, '1'));
  18.  
kind regards
Feb 19 '08 #4
Thank you again. I ended up using regEx and the search() function.
Expand|Select|Wrap|Line Numbers
  1. var contnum = new RegExp('/,?' + contID[i].value + ',?/');
  2. if(dispCA.join().search(contnum)>=0)
  3. {
  4.  
It seems to work okay...
Feb 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
3
by: mdh_2972 | last post by:
I have an array of over 1000 links in a .JS file. I do not want to put the whole thing on my page because it would take to long to render the page. So how can I randomly pick 1 element from the...
16
by: Computer geek | last post by:
Hello, I am new to VB.NET and programming in general. I have taught myself a lot of the basics with vb.net but am still quite the novice. I am working on a little application now and I need some...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
1
by: Someone21 | last post by:
Hi all, I am currently trying to retrieve a string from a textfile, separate that and put it in an array. I stumbled on the problem that *token is a pointer, and not the string. However when i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.