使用正则表达式从字符串中提取数字

时间:2016-05-25 18:46:30

标签: regex

我需要在字符串前面提取数字前提取数字。

THIS IS THE STRING:
-----
DATABASE CONNECTION SUCCEEDED
Executed: SELECT MIN(written), COUNT(written) as ts FROM BGS.SettlementQueue
0 items are in the queue
-----

我需要在"前面找到数字;项目在队列中#34;。 该数字的范围为0至9999

TIA

1 个答案:

答案 0 :(得分:0)

假设您的字符串包含您提到的确切文本,您可以使用前瞻来简单地找到“项目在队列中”之前出现的数字:

// Retrieve the number of items that appears prior to " items are in the queue"
var items = input.match(/\d+(?= items are in the queue)/);

示例

var input = "DATABASE CONNECTION SUCCEEDED Executed: SELECT MIN(written), COUNT(written) as ts FROM BGS.SettlementQueue 10 items are in the queue";
console.log(input.match(/\d+(?= items are in the queue)/) + ' is the number of items');