正確的正則表達式模式我想實現一個正則表達式這種模式,但我不能成功,使其工作,我將它喜歡製作在JS
var strInput = $("#inputFilter").val();
var pattern = new RegExp("(#[a-z0-9]{3,})|(@[a-zA-Z0-9]{6,16})|([a-zA-Z0-9]{6,16})(,((#[a-z0-9]{3,})|(@[a-zA-Z0-9]{6,16})|([a-zA-Z0-9]{6,16})))*");
var result = pattern.test(strInput);
if (!result)
{
alert("Must follow this pattern, order is not relevant, e.g Title,#tag,@Username or Title,#tag,@Username,#tag2");
return
}
標題由匹配模式[a-zA-Z0-9] {6,16}因此在這個例子中,它將是與模式匹配的詞HelloWorld
#tag與模式#[a-z0-9] {3,}所以在這個例子中這將是#hello已經匹配的圖案
字@用戶名與模式@ [a-zA-Z0-9] {6,16}相匹配,因此在這個例子中,它將是@MyUser這個與模式相匹配的詞
我想實現的是獲取輸入的匹配是這樣的:
的HelloWorld,#標籤1,#標籤2,#標籤3,@名爲myUsername,#onemoretag,#andonemore
但順序並不重要,也像這樣將被罰款
個#標籤1,#標籤2,#標籤3,@名爲myUsername,#TAG4,HelloWorld的
should match:
#hello
HelloWorld
@username
#hello,@MyUser,HelloWorld
@MyUsername,HiWorld,#hello1,#hello2
should NOT match:
#@hello
#hi
@User
@Username#hello
感謝您的幫助
這不是完全明顯有什麼事情應該和不應該匹配;編寫正則表達式很容易,但我無法確切地告訴你想要它做什麼。你的錯誤信息說'標題'必須首先出現,但是你的任何例子都沒有提供這種形式。逗號分隔的部分是否允許以任何順序發佈?是否允許其中許多人的最低或最高限制? –
我添加了一些更多的信息,讓你更清楚 – Manfredi6