regexstr-replaceucfirst

Capitalize after point and space only the next word after point ( maybe regex + ucfirst )


Capitalize after point and space only the next word after point, maybe using regex + ucfirst..

preg_match('/[\s\.][a-z]/'
$theCode = str_replace(ucfirst.....

In summary is to do uppercase only in the first letter after spaço + point, in all data of the variable.

$theCode = 'babab. babab babab. bababa bababa bababa. bababa babab baba';

out:

$theCode = 'babab. Babab babab. Bababa bababa bababa. Bababa babab baba';

No matter what method I adopted, I just suggested.

Thanks


Solution

  • For php, preg_replace_callback + ucfirst

    live example

    $theCode = 'babab. babab babab. bababa bababa bababa. bababa babab baba';
    $pattern = '/([a-z][^.]*)/i';
    
    $result = preg_replace_callback($pattern, function($matches) {
        return ucfirst($matches[0]);
    }, $theCode);
    
    echo $result;
    

    For javascript,

    function capitalizeAll(str) {
       return str.replace(/([a-z])([^.]*)/gi, (a, b, c) => {
          return (b || '').toUpperCase() + c;
       });
    }
    
    var theCode = 'babab. babab babab. bababa bababa bababa. bababa babab baba';
    
    console.log(capitalizeAll(theCode));