Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输入表单验证中的javascript正则表达式替换_Javascript_Regex_Forms_Validation - Fatal编程技术网

输入表单验证中的javascript正则表达式替换

输入表单验证中的javascript正则表达式替换,javascript,regex,forms,validation,Javascript,Regex,Forms,Validation,输入格式为的字符串时: 开头是r或r,然后是一些非数字字母,我想 将字符串替换为R 在这种情况下: onkeyup="this.value=this.value.replace(/^[rR]\D/g,'R')" 但它不起作用 有人能帮我吗?谢谢。使用以下正则表达式: onkeyup="this.value=this.value.replace(/^[rR]\D+/, 'R')"; 请参阅此演示: ?发布您可能的输入和预期的输出,方法如下this.value.replace(/^[

输入格式为的字符串时:

开头是
r
r
,然后是一些非数字字母,我想 将字符串替换为
R

在这种情况下:

onkeyup="this.value=this.value.replace(/^[rR]\D/g,'R')"
但它不起作用


有人能帮我吗?谢谢。

使用以下正则表达式:

     onkeyup="this.value=this.value.replace(/^[rR]\D+/, 'R')";
请参阅此演示:


?发布您可能的输入和预期的输出,方法如下
this.value.replace(/^[^r^r]/g,'r')
?@ArunPJohny的JSFIDLE确实适合我(在Chrome上测试)。您甚至不需要
g
修饰符,因为从
^
开始的正则表达式最多只匹配一次(无需多次替换)。
/^/
从不需要g标志,有多少个“字符串开始”?;-)谢谢,chsdk。这就是我想要的。@ArunPJohny的答案也很好。谢谢大家。太好了,很高兴能帮上忙。