简体   繁体   English

用于小写字母开头的驼峰格式的REGEX模式

[英]REGEX Pattern for Camel case format starting with lowercase

I need to write a validation in javascript that will detect the input value is in camelcase format. 我需要用javascript编写验证,以检测输入值是否为驼峰格式。 The first letter expected to be lowercase. 首字母应为小写。

I got some code from stackflow and made it in JSFIDDLE. 我从堆栈流中获取了一些代码,并在JSFIDDLE中进行了编写。

Its working for both helloWorld and HelloWorld 它适用于helloWorld和HelloWorld

I would appreciate if someone could help me to complete the task.. 如果有人可以帮助我完成任务,我将不胜感激。

function myFunction() {
var str = "HeLloWorld";
var patt = new RegExp("[a-z]([a-z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*");
var res = patt.test(str);
document.getElementById("demo").innerHTML = res;
}

Note: I have been playing around with it. 注意:我一直在玩它。 So it will not be the correct regex. 因此,它将不是正确的正则表达式。

I have not delved too deep into the pattern, but it seems working for your input only when you add the boundaries or anchors. 我没有深入研究该模式,但是它似乎仅在您添加边界或锚点时才对您的输入有用。 The problem with matching HelloWorld is that your pattern matched elloWorld and RegExp#test() returned true. 匹配HelloWorld的问题是您的模式匹配elloWorldRegExp#test()返回true。

^[a-z]([a-z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*$‌​

Or 要么

\b[a-z]([a-z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*\b

See the regex demo 正则表达式演示

 function myFunction() { var str = "HelloWorld"; var patt = /\\b[az]([a-z0-9]*[az][a-z0-9]*[AZ]|[a-z0-9]*[AZ][A-Z0-9]*[az])[A-Za-z0-9]*\\b/; var res = patt.test(str); document.getElementById("demo").innerHTML = "HelloWorld match with word boundaries: " + res + "<br/>"; var patt2 = /^[az]([a-z0-9]*[az][a-z0-9]*[AZ]|[a-z0-9]*[AZ][A-Z0-9]*[az])[A-Za-z0-9]*$/; document.getElementById("demo").innerHTML += "HelloWorld match with start/end string anchors: " + patt2.test(str); } 
 <p>The test() method returns true if it finds a match, otherwise it returns false.</p> <p>Click the button to search a string for the character "e".</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM