【问题标题】:Search text on javascript在 javascript 上搜索文本
【发布时间】:2011-10-25 17:10:32
【问题描述】:

如何在 javascript 上搜索文本。

var v=20;
str = "The rain 20 in SPAIN stays mainly in the plain";
var patt1=/ain  v/gi;
var n = str.match(patt1);
alert(n);

我正在尝试但没有得到任何结果。 我哪里错了,哪位大侠能帮帮我。

【问题讨论】:

  • str 是什么?它没有定义
  • 你要搜索什么?
  • 抱歉,我更新了我的问题,请再次查看。
  • 我想搜索 ain 20 但 ain 是不变的,20 来自 throe var v.

标签: javascript jquery


【解决方案1】:
var v = 20; // Variable part of search query
var str = "The rain 20 in SPAIN stays mainly in the plain"; // String to search
var n = str.match('ain ' + v); // The searching, returns null if not found or the matches found. Notice the 'ain' string has been added (this can of course be changed)

if(n != null){ // if not null
   alert(n + ' found!');
}else{ // if null
   alert(v + ' not found...');   
}

我想这会对你有所帮助:http://jsfiddle.net/xFeAH/

【讨论】:

    【解决方案2】:

    我可能错过了重点,因为问题非常广泛,但您也可以使用str.indexOf(findThisString)

    【讨论】:

      【解决方案3】:
      var v=20;
      var str = "The rain 20 in SPAIN stays mainly in the plain";
      var patt1=new RegExp("ain " + v, "gi");
      var n = str.match(patt1);
      alert(n); // alerts 'ain 20'
      

      Example

      【讨论】:

        猜你喜欢
        • 2015-10-31
        • 2013-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-27
        • 2015-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多