簡體   English   中英

在JavaScript上搜索文字

[英]Search text on javascript

如何在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);

我正在嘗試但沒有得到任何結果。 哪里錯了,任何伙伴都可以幫助我。

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'

我可能會str.indexOf(findThisString)這一點,因為問題非常廣泛,但你也可以使用str.indexOf(findThisString)

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM