JavaScript RegExp:R 命名约定
我们都熟悉 R 中的命名约定(如果不熟悉的话:Venables & Smith - R 简介,第 1.8 章)。另一方面,正则表达式仍然是未知领域,也是我迄今为止编程生活中最讨厌的部分……最近,我正式开始 JavaScript 重演,我正在尝试创建精确的 JavaScript 重述。 RegExp 用于检查正确的 R 对象名称。
简介: 对象名称必须以 .
或小写/大写字母开头,如果以 .
开头,则不能以数字继续...之后,允许使用字母数字符号。
和下划线_
。
长话短说,这是我的 JS 代码:
var txt = "._.";
var inc = /^\.(?!\d)|^[a-z\.]/i;
document.write(inc.test(txt));
这种方法管理第一部分(.
和/或小/大写字母和 .
之后的数字),但我无法传递类似的内容<代码>& [\w\.]。我可以编写一个函数来处理这个问题,但是是否可以使用单个 RegExp 来管理这个问题?
We're all familiar with naming conventions in R (if not: Venables & Smith - Introduction to R, Chapter 1.8). Regular expressions, on the other hand, remain terra incognita and the most hated part in my programming life so far ... Recently, I've officially started JavaScript recapitulation, and I'm trying to create precise RegExp to check correct R object name.
Brief intro:
Object names must start with .
or lower/uppercase letter, and if they start with .
cannot continue with numeric... afterward, alphanumeric symbols are allowed with .
and underscore _
.
Long story short, here's my JS code:
var txt = "._.";
var inc = /^\.(?!\d)|^[a-z\.]/i;
document.write(inc.test(txt));
This approach manages the first part (.
and/or lower/upper case and numeric after .
), but I cannot pass something like & [\w\.]
. I can write a function that will take care of this one, but is it at all possible to manage this with a single RegExp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 R 或其命名约定,但我会尝试一下:
如果您只是想验证名称开头是否正确,您所需要做的就是删除来自字符类的
\.
,留下:/^\.(?!\d)|^[az]/i
。否则,.
仍可能是第一个字符,对其余字符没有限制。如果您想验证整个名称是否正确,类似这样的操作应该有效:
I'm not familiar with R or its naming conventions, but I'll give it a shot:
If you're only trying to verify that the name begins correctly, all you need to do is remove the
\.
from the character class, leaving you with:/^\.(?!\d)|^[a-z]/i
. Otherwise, the.
may still be the first character with no restrictions on the remaining ones.If you want to verify the that entire name is correct, something like this should work: