簡體   English   中英

正則表達式只允許大括號內的字母數字字符和下划線和特定占位符

[英]Regex that will only allow alphanumeric characters and underscore and specific placeholder inside curly braces

我想要一個只允許字母數字字符和下划線以及花括號內的特定占位符的正則表達式。

有效示例:

test{placeholder}
test_{placeholder}
test_123_{placeholder}
test
test_123
test123
{placeholder}_test
test{placeholder}test
And any combination of above.

這就是我想出的:

[^-A-Za-z0-9_]|^\{placeholder\}

我的理解方式是:

[^-A-Za-z0-9_] - 不允許使用除 az 0-9 和下划線之外的任何其他字符。

|^\\{placeholder\\} - 或者任何沒有說 {placeholder} 的東西

但它不起作用,我不知道為什么。

這是演示

請幫忙。

您可以使用

^(?:[A-Za-z0-9_]|{placeholder})+$

細節

  • ^ - 字符串的開始
  • (?: - 非捕獲組的開始:
    • [A-Za-z0-9_] - 一個字字符:字母、數字、 _
    • | - 或者
    • {placeholder} - 一個特定的子字符串
  • )+ - 組結束,重復 1 次或多次
  • $ - 字符串的結尾。

請參閱正則表達式演示正則表達式

在此處輸入圖片說明

暫無
暫無

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

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