Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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/18.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 camelCase函数中的连字符_Javascript_Regex - Fatal编程技术网

正则表达式/javascript camelCase函数中的连字符

正则表达式/javascript camelCase函数中的连字符,javascript,regex,Javascript,Regex,函数应将字符串作为参数,并对其进行大小写转换。我在使用正则表达式和string.replace方法时遇到连字符问题 camelCase的“最新技术”应返回“最新技术” 别担心,京子应该回唐太瑞代子 以下内容适用于这两种情况,但我想让它干燥,去掉连字符if子句并将连字符大小写包含在.replace中,然后返回 function camelCase(phrase) { let re = /[a-z]+/i; let hyphens = /[-+]/g if(typeof phras

函数应将字符串作为参数,并对其进行大小写转换。我在使用正则表达式和string.replace方法时遇到连字符问题

camelCase的“最新技术”应返回“最新技术” 别担心,京子应该回唐太瑞代子

以下内容适用于这两种情况,但我想让它干燥,去掉连字符if子句并将连字符大小写包含在.replace中,然后返回

function camelCase(phrase) {    
 let re = /[a-z]+/i;
 let hyphens = /[-+]/g
   if(typeof phrase !== 'string' || !phrase.match(re) || !phrase || phrase === null){
   return "Please enter a valid string.";
   } else if (phrase.match(hyphens)){
    return phrase.toLocaleLowerCase();
   }else{
     return phrase.replace(/(?:^\w+|[A-Z]|\s+\w)/g, function(letter, index) {
       return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
       }).replace(/\W+/g, '');
       }
    }
console.log(camelCase('state-of-the-art')) // 'state-of-the-art'
console.log(camelCase("Don't look back"))  // dontLookBack
在没有连字符if子句的情况下,我们可以使连字符case工作吗? 另外,我觉得camelCasedon lOOk_应该是索引大于0的小写字母,但在控制台中似乎没有这样做


有人想帮忙吗?为了处理连字符问题,您可以考虑使用[\W-]或[^ \W-]适当的字母数字类的一部分。 要将所有非首字母小写,我建议在适当的情况下将所有单词与\S\S*大写$1和小写$2匹配:

功能短语{ 返回短语.replace/[^\w-]*\S\S+/g,函数u1,first,rest,index{ 返回索引?first.toUpperCase:first.toLowerCase +rest.toLowerCase; }.replace/[^\w-]+/g; } console.LogCase是最先进的; console.logcamelCase-state-of-art; 控制台。不要回头看; 控制台。不要回头看;
控制台。logcamelCase???不要回头看 您可以通过添加否定的前瞻断言使连字符工作。替换/-\W+/g;。这将告诉它替换除-破折号字符以外的所有非单词字符

关于下套管问题:目前决定该情况的唯一标准是它是否出现在管柱的开头。否则,您将使用大写字母,包括大写字母匹配。这也是一个非常简单的修复方法。事情是这样的:

function camelCase(phrase) {
  let re = /[a-z]+/i;
  if (typeof phrase !== 'string' || !phrase.match(re) || !phrase || phrase === null) {
    return "Please enter a valid string.";
  } else {
    return phrase.replace(/(?:^\w+|(\s+)\w)|[A-Z]/g, function (letter, sp, index) {
      console.log(letter, sp, index);
      return (index == 0 || sp === undefined) ? letter.toLowerCase() : letter.toUpperCase();
    }).replace(/(?!-)\W+/g, '');
  }
}
更改说明:

更改短语中的关联顺序。替换regexp。我们希望空格单词组合优先于大写匹配。 将捕获组添加到空间中,以便我们更好地了解捕获是否跟随空间 更改布尔表达式:我们希望它是小写的,如果: 它是第一个字符索引===0 或者没有空格匹配这将是[a-Z]匹配,没有前面的空格 顺便说一句,您似乎不会在下划线字符上使用驼峰式大小写,而只在空格上使用驼峰式大小写。这是故意的吗?我从来没有见过不转换蛇格下划线的驼峰格例程。

应该让您开始了,我没有尝试过。