Javascript 仅匹配alpha字符的正则表达式变量
我有一个不起作用的javascript:Javascript 仅匹配alpha字符的正则表达式变量,javascript,regex,Javascript,Regex,我有一个不起作用的javascript: function test(id) { if (id.match('^(.+?)#')) { alert(RegExp.$1); } } test('test#f'); // should alert 'test' test('tes4t#f'); // should not alert 我只想匹配出现在#前面的a-zA-Z字符。我尝试调整regexp,使其成为(.+?)[a-zA-Z],但我有一种不正确
function test(id) {
if (id.match('^(.+?)#')) {
alert(RegExp.$1);
}
}
test('test#f'); // should alert 'test'
test('tes4t#f'); // should not alert
我只想匹配出现在
#
前面的a-zA-Z
字符。我尝试调整regexp,使其成为(.+?)[a-zA-Z]
,但我有一种不正确的感觉。这就是regex 101:
var m = id.match(/^([a-zA-Z]+)#/);
if (m) alert(m[1]);
在Javascript中,正则表达式是在斜杠之间定义的
另外,懒惰量词在这里也没有用。我还没有测试过性能,但应该没有什么区别
最后,利用
match
的返回值,它返回并使用完整的数学表达式进行数组,然后是捕获的组。这就是正则表达式101:
var m = id.match(/^([a-zA-Z]+)#/);
if (m) alert(m[1]);
在Javascript中,正则表达式是在斜杠之间定义的
另外,懒惰量词在这里也没有用。我还没有测试过性能,但应该没有什么区别
最后,利用
match
的返回值,它返回并使用完整的数学表达式进行数组,然后是捕获的组。这就是正则表达式101:
var m = id.match(/^([a-zA-Z]+)#/);
if (m) alert(m[1]);
在Javascript中,正则表达式是在斜杠之间定义的
另外,懒惰量词在这里也没有用。我还没有测试过性能,但应该没有什么区别
最后,利用
match
的返回值,它返回并使用完整的数学表达式进行数组,然后是捕获的组。这就是正则表达式101:
var m = id.match(/^([a-zA-Z]+)#/);
if (m) alert(m[1]);
在Javascript中,正则表达式是在斜杠之间定义的
另外,懒惰量词在这里也没有用。我还没有测试过性能,但应该没有什么区别
最后,利用match
的返回值,它返回完整的数学表达式并进行数组,后面是捕获的组。尝试以下操作:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
说明:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/; // This constructs a regex, notice it is NOT a string literal
// Gets the match followed by the various capture groups, or null if no match was found
var match = id.match(rmatch);
if (match) {
// match[1] is the first capture group, alert that
alert(match[1]);
}
}
试试这个:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
说明:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/; // This constructs a regex, notice it is NOT a string literal
// Gets the match followed by the various capture groups, or null if no match was found
var match = id.match(rmatch);
if (match) {
// match[1] is the first capture group, alert that
alert(match[1]);
}
}
试试这个:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
说明:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/; // This constructs a regex, notice it is NOT a string literal
// Gets the match followed by the various capture groups, or null if no match was found
var match = id.match(rmatch);
if (match) {
// match[1] is the first capture group, alert that
alert(match[1]);
}
}
试试这个:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
说明:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/; // This constructs a regex, notice it is NOT a string literal
// Gets the match followed by the various capture groups, or null if no match was found
var match = id.match(rmatch);
if (match) {
// match[1] is the first capture group, alert that
alert(match[1]);
}
}
使用if(id.match(/^([a-zA-Z]+)#/)
更新了我的答案,因为match
需要正则表达式参数,而不是字符串
此外,由于某种原因,id.match(/^([A-z]+)#/)
匹配^test
。为什么?使用if(id.match(/^([a-zA-Z]+)#/)
更新了我的答案,因为match
需要正则表达式参数,而不是字符串
此外,由于某种原因,id.match(/^([A-z]+)#/)
匹配^test
。为什么?使用if(id.match(/^([a-zA-Z]+)#/)
更新了我的答案,因为match
需要正则表达式参数,而不是字符串
此外,由于某种原因,id.match(/^([A-z]+)#/)
匹配^test
。为什么?使用if(id.match(/^([a-zA-Z]+)#/)
更新了我的答案,因为match
需要正则表达式参数,而不是字符串
此外,由于某种原因,id.match(/^([A-z]+)#/)
匹配^test
。为什么?试试这个:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
功能测试(id){
var regex=/([a-z]+)#/i,
组=regex.exec(id);
if(组和组[1]){
警报(组[1]);
}
}
这意味着(使用paren)捕获一组一个或多个字母([a-z]+),后跟一个散列,并使匹配不区分大小写(因为末尾有i)。尝试以下方法:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
功能测试(id){
var regex=/([a-z]+)#/i,
组=regex.exec(id);
if(组和组[1]){
警报(组[1]);
}
}
这意味着(使用paren)捕获一组一个或多个字母([a-z]+),后跟一个散列,并使匹配不区分大小写(因为末尾有i)。尝试以下方法:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
功能测试(id){
var regex=/([a-z]+)#/i,
组=regex.exec(id);
if(组和组[1]){
警报(组[1]);
}
}
这意味着(使用paren)捕获一组一个或多个字母([a-z]+),后跟一个散列,并使匹配不区分大小写(因为末尾有i)。尝试以下方法:
function test(id) {
var rmatch = /^([a-zA-Z]+?)#/;
var match = id.match(rmatch);
if (match) {
alert(match[1]);
}
}
功能测试(id){
var regex=/([a-z]+)#/i,
组=regex.exec(id);
if(组和组[1]){
警报(组[1]);
}
}
这意味着(使用paren)捕获一组一个或多个字母([a-z]+),后跟一个散列,并使匹配不区分大小写(因为末尾有i)。我见过正则表达式在没有斜杠的情况下工作,它们是强制性的还是有某种用途?@OP也许你在PHP中见过它们,不在Javascript中。表达式
[a-zA-Z]
被称为a。我见过正则表达式不带斜杠工作,它们是必需的还是有某种用途?@OP也许你在PHP中见过它们,而不是在Javascript中见过。表达式[a-zA-Z]
被称为a。我见过正则表达式不带斜杠工作,它们是强制性的还是有某种用途?@OP也许你在PHP中见过它们,而不是在Javascript中。表达式[a-zA-Z]
被称为a。我见过正则表达式在没有斜杠的情况下工作,它们是强制性的还是有某种用途?@OP也许你在PHP中见过它们,不在Javascript中。表达式[a-zA-Z]
被称为a。这被否决了-这个表达式有什么不正确的地方?我测试了它,它似乎检查出来了,好吗?这是被否决的-这个表达式有什么不正确的地方?我测试了它,它似乎检查出来了,好吗?这是被否决的-这个表达式有什么不正确的地方?我测试了它,它似乎检查出来了,好吗?这是被否决的-这个表达式有什么不正确的地方?我测试过了,看起来还不错,好吗?