SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegExp Exact Match on Array String Value

    Hi guys,

    I have this that loops through an array and checks if the elements className matches one of the array values.

    for (var t=0; t<pArrCol.length; t++) {
    if (pObj[i].className.match(pArrCol[t])) {

    it works but I need an exact match not just part of the string.

    For example:

    When I run that code and lets say my array contains:
    var arrCol = ['s25','s121'];
    and the className is "s121".

    Well the problem is it matches both values.

    I need it to check whether it matches the whole string value not just part of it.

    I'm not very good with RegExp but I'm pretty sure that's what the solution is.

    Help would be awesome!

    Thanks!

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just check for equivalence:
    Code:
     for (var t=0; t<pArrCol.length; t++) {
    	if (pObj[i].className ==== pArrCol[t]) {
      //...
    }
    I assume this loop is within another loop?

  3. #3
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depending on your possible values in pArrCol, somthing like this might be good.

    Code:
    var pArrCol = {s25:1, s121:1};
    if(pArrCol[pObj[i].className]){
    // ...
    }
    It really would be nice to see more of what's going on.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •