[英]jQuery regex can't match when string starts with “<” character
我正在嘗試查看字符串的開頭是否具有“ <”字符,但是它將不起作用。 在regex101.com上它可以正常工作 ,但在個人文件中嘗試時卻不能。
鍵入backslash
,它可以正常工作,鍵入<
,它將不起作用。
$(function() { $(document).on('keyup', '.input', function() { var str = $(this).html(); // not working if (str.match(/^</)) { console.log('starts with "<" character'); } // but this works?? if (str.match(/^\\\\/)) { console.log('starts with backslash'); } }); });
.input { border: 1px solid #ccc; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="input" contenteditable></div>
Javascript可以將<
符號編碼為<
。 嘗試做console.log(str);
看看是否正在發生(我在瀏覽器中嘗試過的情況)。
如果是這樣,請將<
符號替換為<
像這樣:
if (str.match(/^</)) {
console.log('starts with "<" character');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.