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.
B开发者_StackOverflow中文版rief 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?
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:
/^(?:\.(?!\d)|[a-z])[a-z0-9_\.]+$/i
精彩评论