簡體   English   中英

正則表達式匹配 4 到 60 個字符、空格、單引號並排除尾隨空格

[英]Regex match 4 to 60 characters, space, single quote and exclude trailing spaces

我需要匹配 4 到 60 個字符、空格、單引號,並排除尾隨空格。

案例:

  1. “aaaa” - 匹配
  2. "tes'" - 匹配
  3. “不” - 不匹配
  4. “asdpijfaousdhfaoijsdgohasd' asdfa adsfads” - 匹配

我需要從此正則表達式中排除尾隨空格

^[\\w\'\ ]{4,60}$

[ \t]+$ - 不知道如何將它添加到這個正則表達式

您可以使用重復部分以空格或制表符開頭的模式。 在模式開始時,您可以使用正向預測來斷言 4 - 60 個字符的長度。

^(?=[\w' \t]{4,60}$)[\w']+(?:[ \t][\w']+)*$

正則表達式演示

 const pattern = /^(?=[\w' \t]{4,60}$)[\w']+(?:[ \t][\w']+)*$/; [ "aa aa", "tes'", "not ", "asdpijfaousdhfaoijsdgohasd' asdfa adsfads" ].forEach(s => console.log(`${s} --> ${pattern.test(s)}`) )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM