想要搜索文本并获得匹配项:
WARN
我正在搜索RegExp()
字并使用{{1}}方法进行模式,
但它不起作用。
答案 0 :(得分:1)
两个问题:
var pattern = new RegExp("/" + text + "/g");
更改为var pattern = new RegExp(text, "g");
RegExp
,希望将这些标志用作第二个参数$('Test').text().
。没有使用$('#Test').text().
function searchText(text) {
var pattern = new RegExp(text, "g");
var totalMatchCount = ($('#Test').text().match(pattern) || []).length;
console.log(totalMatchCount);
document.getElementById('result').innerHTML = JSON.stringify($('#Test').text().match(pattern), 0, 4);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Test">
2015-11-05 22:01:00,062 WARN [dms.framework.licensing.LicenseManager] (Thread-1169 (HornetQ-client-global-threads-643998766)) The license you are using has expired or is bad. value: -5 2015-11-05 22:02:00,026 WARN [dms.framework.licensing.LicenseManager]
(Thread-1165 (HornetQ-client-global-threads-643998766)) The license you are using has expired or is bad. value: -5 2015-11-05 22:03:00,049 WARN [dms.framework.licensing.LicenseManager] (Thread-1180 (HornetQ-client-global-threads-643998766)) The license
you are using has expired or is bad. value: -5 2015-11-05 22:04:00,029
</div>
<button onclick="searchText('WARN')">Search</button>
<hr />
<pre id="result"></pre>
&#13;