jeck貓
2022-10-21 10:49:01
我想知道如何將帶有括號的特殊字符部分(如 {} [] () 和其他內容(如“')添加到我在下面嘗試的唯一字符中,但由于某種原因,當我添加另一個字符時它停止工作.<-- works but does not have any brackets or quotes for special character--><form acton = "#"> <input type="password" id="newPass" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[!@#$%^&*+`~=?\|<>/]).{8,}" <button>submit</button></form>上面的部分有效,但沒有引號或更多特殊字符下面的代碼有括號但不起作用<-- does not work (if you do not enter special character user will be able to submit but it does not have any brackets --><form acton = "#"> <input type="password" id="newPass" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[~`!@#$%^&*()-_=+[]{};:'.,"\|/?><]).{8,}" <button>submit</button></form>這也不起作用(來自答案)<form acton = "#"> <input type="password" id="pass" required pattern="(?=.*\d)(?=.*[a-z])(?=.*?[0-9])(?=.*?[~`!@#$%\^&*()\-_=+\[\]{};:\'.,\"\\|/?\>\<]).{4,}"> <button>submit</button></form>我正在尋找一個使底部(帶括號的那個和我包括的所有特殊字符)有效的答案。
3 回答

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
\]
如果在模式搜索期間需要匹配正則表達式(例如 )字符,您應該轉義它們。另外,請參閱現有答案:
以下對我有用,希望它對你有用:(我只是添加了轉義字符)
<input type="password" id="newPass" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[!@#$%^&*+`~'=?\|\]\[\(\)\-<>/]).{8,}">

MYYA
TA貢獻1868條經驗 獲得超4個贊
你實際上非常接近。最大的錯誤是:
你應該escape
在里面的一些特殊字符character group
并且你應該有hyphen as the last character
(否則它是一個范圍)。
這是您要求的 RegExp:
<input type="password" id="pass" required pattern = "(?=.*\d)(?=.*[a-z])(?=.*?[0-9])(?=.*?[~`!@#$%^&*()_=+\[\]{};:'.,"\\|\/?><-]).{4,}">
更新:我忘了對模式進行 html 編碼以直接在 html 中使用?,F在它應該可以工作了。
添加回答
舉報
0/150
提交
取消