簡體   English   中英

檢查字符串是否包含字符序列和javascript中的未知變量?

[英]Checking if a string contains character sequence and unknown variable in javascript?

是否可以檢查javascript字符串是否包含指定的一系列字符和介於兩者之間的未知字符。 例如:

var secondValue = 9;
var testString = "the result is 8 and " + secondValue + " as shown above";
var stringToTestAgainst1 = "the result is * and " + secondValue  + " as show above";
var stringToTestAgainst2 = "the result is * as show above";

我如何測試它們,以便在將testString與第一個值進行比較時返回true。 但是,將其與第二個進行比較時是錯誤的?

您可以使用正則表達式:

/the result is \d+ as shown above/.test('the result is 8 as shown above')
// => true

請參閱MDN的RegExp文檔

\\d+檢查是否存在一個或多個數字。

編輯

您可以檢查是否存在許多事物,例如\\d代表數字, \\w代表任何字母數字字符,等等。上面的參考提供了許多示例。

暫無
暫無

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

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