Capitalize the first letter of first word inside the brackets

Viewed 286

I want to capitalize the first letter of each string inside the brackets...

If we have this string:

const text = 'This [forest or jungle] is [really] beautiful';

The desired result would be 'This [Forest or jungle] is [Really] beautiful'

So far:

I have the capitalize function:

function capitalize(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
} 

And here we have this regex code to select string inside the brackets:

/(?<=\[)(.*?)(?=\])/g

Please help

4 Answers

You can use

text = text.replace(/(?<=\[)[a-z](?=[^\][]*])/g, (x) => x.toLocaleUpperCase())
// OR
text = text.replace(/(?<=\[)\p{Ll}(?=[^\][]*\])/gu, (x) => x.toLocaleUpperCase())

See the regex demo/regex demo #2. Details:

  • (?<=\[) - a positive lookbehind that matches a location that is immediately preceded with a [ char
  • [a-z] - a lowercase ASCII letter
  • \p{Ll} - any Unicode lowercase letter
  • (?=[^\][]*\]) - a positive lookahead that requires any zero or more chars other than [ and ] and then a ] immediately to the right of the current location.

See the JavaScript demo:

const text = 'This [forest or jungle] is [really] beautiful';
console.log( text.replace(/(?<=\[)[a-z](?=[^\][]*])/g, (x) => x.toLocaleUpperCase()) )

Your pattern already have the match, and you can omit the parenthesis for the capture group. Then you can use replace and use the capitalize function as the callback.

Example using your slightly modified pattern without the capture group:

function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
let text = 'This [forest or jungle] is [really] beautiful';
text = text.replace(/(?<=\[).+?(?=\])/g, capitalize);
console.log(text);

A bit more performant pattern could be using a negated character class

(?<=\[)[^\][]+(?=\])

Regex demo

The pattern matches:

  • (?<=\[) Assert a [ at the left
  • [^\][]+ Match 1+ times any char except [ or ]
  • (?=\]) Assert a ] to the right

Or with the negated character class

const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1);
let text = 'This [forest or jungle] is [really] beautiful';
text = text.replace(/(?<=\[)[^\][]+(?=\])/g, capitalize);
console.log(text);

For the question above, try the following code:

let a = (x) => {let y = x.split('');y.forEach((z,i) => {if(z == '['){y[i+1]=y[i+1].toUpperCase()};});return y.join('');}

Split it into an array and convert the next finding word to uppercase.

make e.g. use of string replacement and a regex which matches braces and captures the text in between them, like ... /\[([^\]]+)\]/g ... which reads ...

  • / ... /g ... globally ...
  • ... match an opening bracket \[ ...
  • ... then capture at least one character (or a sequence of it) which is not a closing bracket ([^\]]+) ...
  • ... then match an closing bracket \].

const textSample = 'This [forest or jungle] is [really] beautiful.';
const regXCapture = (/\[([^\]]+)\]/g);

console.log(
  textSample.replace(regXCapture, (match, capture) =>
    `[${ capture[0].toLocaleUpperCase() }${ capture.slice(1) }]`
  )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Related