![](/img/trans.png)
[英]How to check if the user has not entered the data to a form (befor sending)?
[英]How to count the amount of specifc symbols that a user has entered in a text form
抱歉,如果這似乎是一個新手問題,但我想知道是否有辦法計算一個人輸入的 URL 樣式格式中特定符號的數量,例如點或斜線。
var regex = /https?:\/\/(www\.)?[A-Za-z=:.]{1,12}\.[A-Za-z]{1,6}\b([-A-Za-z0-9%_\+=?&//#]){1,250}/
var validx = regex.test(document.getElementById("webname").value);
這就是正則表達式以及我如何調用文本輸入。 因此,我想知道是否有一種方法可以計算用戶輸入的符號數量。
我以為我可以調用包含檢查器的 function 但它不起作用
function dotcheck()
{
var dots = detect.webname.value;
if (dots.indexOf(".") > 0){}
function 我認為你可以看到/計算那里的點數
if (dotcheck = false){
messages.innerHTML = 'Safe';
我曾經調用 function 的 if 語句。
任何幫助將不勝感激,如果您還需要我提供任何其他幫助,請不要猶豫。
String
中任何字符的計數就是String.match(RegExp)
返回的匹配數。
由於您特別提到了句點,並且這些是特殊的RegExp
符號,因此您不能只為RegExp
編寫/./g
。 你必須用\
來逃避它,就像這樣:
/\./g
附加到末尾的g
標志意味着獲取所有匹配項,而不僅僅是第一個匹配項。
const string = 'https://stackoverflow.com/questions/62008355'; const slashes = /\//g; const count = string.match(slashes).length; console.log(count); // 4 slashes
const string = 'https://stackoverflow.com/questions/62008355'; const periods = /\./g; const count = string.match(periods).length; console.log(count); // 1 period
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.