te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>regex - Convert "Sentence case" to "camelCase" in JavaScript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Convert "Sentence case" to "camelCase" in JavaScript - Stack Overflow

programmeradmin4浏览0评论

Having a difficult time finding a solution to this, there are several solutions for the reverse.

I have considered replacing every " " and the following first character with an uppercase version of itself:

value.toLowerCase().replace(/\s+/g, function (g) { return g[1].toUpperCase() })

Only, the regex /\s+/g needs to be changed to match the first character.

If there is an existing question that is exactly this, provide link and I will close this myself. I can't find a solution on SO


Examples:

"I walk my dog to the park" or "i Walk my DOG to the Park" => "iWalkMyDogToThePark"

Having a difficult time finding a solution to this, there are several solutions for the reverse.

I have considered replacing every " " and the following first character with an uppercase version of itself:

value.toLowerCase().replace(/\s+/g, function (g) { return g[1].toUpperCase() })

Only, the regex /\s+/g needs to be changed to match the first character.

If there is an existing question that is exactly this, provide link and I will close this myself. I can't find a solution on SO


Examples:

"I walk my dog to the park" or "i Walk my DOG to the Park" => "iWalkMyDogToThePark"

Share Improve this question edited Jul 23, 2014 at 16:49 TaylorMac asked Jul 23, 2014 at 16:34 TaylorMacTaylorMac 9,00221 gold badges77 silver badges105 bronze badges 2
  • Can you provide a plete example of the intended result? – Austin Brunkhorst Commented Jul 23, 2014 at 16:39
  • An example would be better. – Avinash Raj Commented Jul 23, 2014 at 16:39
Add a ment  | 

6 Answers 6

Reset to default 9

You need to catch the next character. You can use (.) or ([a-z])

var toCamelCase = function(string){
  return string.replace(/\s+(.)/g, function (match, group) { 
    return group.toUpperCase()  
  })
}

Maybe you could use this:

function camelCase(value) { 
    return value.toLowerCase().replace(/\s+(.)/g, function(match, group1) {
        return group1.toUpperCase();
    });
}

(taken from here)

You can use this camel case conversion code:

function toCamelCase(str) {
  return str.replace(/(?:^.|[A-Z]|\b.)/g, function(letter, index) {
    return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
  }).replace(/\s+/g, '');
}

var val = toCamelCase("Sentence case");
//=> sentenceCase

val = toCamelCase('hello how are you');
//=> helloHowAreYou

Are you looking for something like this:

var string = "Hello there what are You doing yes";
string.replace(/([A-Z])([a-z]+)\s+([a-z])([a-z]+)/g, function($1, $2, $3, $4, $5) { 
    return $2.toLowerCase() + $3 + $4.toUpperCase() + $5; 
});

This prints out "helloThere what are youDoing yes".

I think a simpler solution can be achieved without Regex. Just split the string from spaces, and join them with the appropriate casing.

var value = '...';

var camelCase = value.split(' ').map(function(word, i) {
    return (word[0] || '')[i == 0 ? 'toLowerCase' : 'toUpperCase']() +
           word.substr(1).toLowerCase();
}).join('');

JSFiddle

I ended up doing the following (ES6):

function camelCase (str) {
    return str.split(/[^a-zA-Z0-9]/g).map((x, index) => {
        if (index === 0) return x.toLowerCase()
        return x.substr(0, 1).toUpperCase() + x.substr(1).toLowerCase()
    }).join('')
}

It takes the assumption that in general, the aim to convert to camelCase is for variables, so no accents or odd chars should be present (or will be split).

camelCase("I walk my dog to the park")
> "iWalkMyDogToThePark"
发布评论

评论列表(0)

  1. 暂无评论