English 中文(简体)
查找用于验证术语(关键字)输入的正则表达式
原标题:find regex for validating terms (keyword) input

不幸的是,我的正则表达式很差!你能指导我用javascript写一个正则表达式吗?它可以决定我的术语输入框。用户应该以这种格式输入术语:

#(all alphanumeric chars + blank + dash + quotation )

例如:

#keyword1#key word2#keyword3#key-word4#key word5

并且这些输入应该是非法的:

#####
##keyword1#key2#
# #keyword
#!%^&
最佳回答
/#[a-zA-Z0-9][a-zA-Z0-9  -]+/

当你说“##关键字”应该无效时,我假设你的意思是“#”应该无效,“#关键字”应该从该字符串中提取。第一个框表示关键字始终以小写字母、大写字母或数字开头。如果这太严格了,并且你想允许例如“#-keyword”,只需在第一个方括号前加上短划线,如下所示:

/#[a-zA-Z0-9-][a-zA-Z0-9  -]+/

要用javascript返回一组结果,请使用“global”修饰符(第二个斜杠后面的g)将其应用于字符串:

arrayOfKeywords = keywordString.match(/#[a-zA-Z0-9-][a-zA-Z0-9  -]+/g);

您可能希望在我的测试页。Regular-expressions.info是一个了解更多正则表达式的有用网站。它们还有一个交互式页面,用于测试上的正则表达式,这在玩游戏时很有用。

问题回答

正如您所写的,术语由以下内容指定:

/#[a-zA-Z0-9  -]+/

重复该模式,并强制其包含带有^$的字符串的开头和结尾。

/^(#[a-zA-Z0-9  -]+)+$/




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签