2013-11-09 93 views
0

如何返回u_c_pages中第一位的url部分,其内容部分匹配模式(不区分大小写)?如果没有找到页面,则应返回空字符串。返回url部分

例如举例如下

var pg = [ "|www.cam.ac.uk|Cambridge University offers degree programmes and world class research." , "!www.xyz.ac.uk!An great University" , "%www%Yet another University" ] 
var pt = "alt"; 

url1(pages,"ALT") returns "www.xyz.ac.uk" 
url1(pages,"xyz") returns "" 

这就是我迄今为止所做的。目前它只筛选出分隔符“|”但我想它来检查任何符号(而不是诉诸正则表达式)...

function url1_m1(u_c_pages, pattern) { 

    // we create an array to store all the arrays that have the seperator "|" 
    // this has been done for situations in which there are invalid arrays lacking the seperator "|" 
    // if we find the seperator "|" within an array we also need to know about the position of "|" in the array element 
    var seperator = []; 
    var seperatorPos = []; 
    if (pattern) { 

     // looping through the u_c_pages to find occurences of seperator "|" 
     // the found variable is initialised with a value of true 
     // if we do not find the seperator "|" in an array element then we set the found variable to false 
     for (var i = 0; i < u_c_pages.length; i++) { 
      var found = true; 
      if ((u_c_pages[i].indexOf("|")) < 0) { 
       found = false; 
      } 

      // whereas if we do find a seperator "|" in an array element 
      // we create a new array element in seperator containing the page index 
      // we create a new array element containing the position of the seperator "|" 
      else { 
       seperator[seperator.length] = i; 
       seperatorPos[seperatorPos.length] = (u_c_pages[i].indexOf("|")); 
      } 
     } 

     // if no arrays with the seperator "|" have been found then we end the program 
     if (seperator.length == 0) { 
      return (""); 
     } 

     // otherwise we initialise variable found2 
     // we loop through the pages with the seperator "|" as we have stored the index of all the pages in array seperator 
     // using the stored index of in array seperator we check the content (after the seperator) for the pattern 
     // we also jump the seperator "|" so it is not returned as one of the results 
     else var found2 = ""; { 
      for (var j = 0; j < seperator.length; j++) 

      // if the the pattern has been found then we extract the url (before the seperator) 
      // break the loop once it has been found 
      // return URL to user 
      { 
       if (u_c_pages[seperator[j]].substring(seperatorPos[j] + 1, u_c_pages[j].length).toLowerCase().indexOf(pattern.toLowerCase()) >= 0) { 
        found2 = (u_c_pages[j].substring(0, seperatorPos[j])); 
        break; 
       } 
      } 
      return (found2) 
     } 
    } 
    else { 
     return (""); 
    } 
} 

alert(url1_m1(pg, pt)); 
+0

你为什么要避免正则表达式? –

+0

使用正则表达式,这可以成为一个单线程:http://regexpal.com/?flags=g®ex=%5E%28.%29%28.%2A%3F%29%5C1&input=%7Cfoo%7Cbar –

回答

1

有实现它的一个非常简单的方法:

function returnData(arr, splitPattern) { 

    var result = [] 
    for (x in arr) { 
    current = arr[x].split(splitPattern); 
    var url = current[1]; // www.cam.ac.uk 
    var alt = current[2]; // Cambridge University offers degree programmes and world class research. 
    result.push({ 
     url: url, 
     alt: alt 
    }) 
    } 
    return result 
} 

var arr = ["|www.cam.ac.uk|Cambridge University offers degree programmes and world class research.", 
"|www.xyz.ac.uk|An great University"]; 

returnData(arr, "|")[0].url // www.cam.ac.uk 

returnData(arr, "|")[0].alt // Cambridge University offers degree programmes and world class research. 

returnData(arr, "|")[1].url // www.xyz.ac.uk!An great University 

returnData(arr, "|")[1].alt // An great University 

var anotherArr = ["!%www.cam.ac.uk!%Cambridge University offers degree programmes and world class research.", 
"!%www.xyz.ac.uk!%An great University"]; 

returnData(anotherArr, "!%")[0].url // www.cam.ac.uk 

returnData(anotherArr, "!%")[0].alt // Cambridge University offers degree programmes and world class research. 

returnData(anotherArr, "!%")[1].url // www.xyz.ac.uk!An great University 

returnData(anotherArr, "!%")[1].alt // An great University 

http://jsfiddle.net/QsBxD/5/

+0

这只会分裂“|” - 如何使用其他符号? – theshizy

+0

其次它需要从功能内工作 – theshizy

+0

我编辑了我的答案,但现在我看到我错过了你所需要的,给我几分钟来重写这个 – silicakes