逗号分隔文本的正则表达式 [英] Regex for comma separated text

查看:177
本文介绍了逗号分隔文本的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个文本字段,用于添加以逗号分隔的标签.(例如 php、jquery、js、ruby on rails)该字段类似于 stakoverflow 上的字段,您可以在其中为帖子添加标签.

I create a text field for adding tags separated by commas. (e.g. php, jquery, js, ruby on rails) The field is like the one on stakoverflow where you add tags for posts.

我想验证输入以确保已正确输入标签.这意味着用户只能输入字母、数字、空格和逗号.我做了以下 regex_match 但我不确定它是否正确.

I want to validate the input to ensure that tags have been entered correctly. This means that users may enter only letters, numbers, spaces, and commas.I made the following regex_match but I'm not entirely sure if it's correct.

regex_match[/^[a-z, ]+$/i]

如果输入包含除我在正则表达式中添加的内容之外的任何内容,我在提交表单时会收到验证错误.我已经测试过,当我放置像 ' " ; \

If the input contains anything but what I added in the regex I get a validation error when I submit the form. I have tested and it works when I put symbols like ' " ; \

这是我的第一个正则表达式,我做对了吗?语言是 PHP.

This is my first regex, am I doing it right? The language is PHP.

推荐答案

好的,这里有几个问题:

OK, there are a few problems here:

  1. 没有名为regex_match"的 php 函数
  2. 函数使用 () 而不是 []
  3. 变量以 $ 开头.[] 里面是一个索引.

您发布的行将导致 PHP 中的语法错误.由于您对其进行了测试",这意味着这实际上不是您使用的代码.请贴出实际代码.

The line you posted will result in a syntax error in PHP. Since you "tested it", it means this is not really the code you used. Please post the actual code.

关于正则表达式,这是一个很好的第一次尝试,但你忘记了数字:

About the regex, it's a good first try, but you forgot the numbers:

/^[a-z,0-9 ]+$/i

这篇关于逗号分隔文本的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆