Tuesday 25 July 2023

How to extract all words that start with a capital letter from the given string "CameraReturnImageSaveLocation" in JavaScript and concatenate with one space in sentence?

 To extract all words that start with a capital letter from the given string "CameraReturnImageSaveLocation" in JavaScript and concatenate them with one space in a sentence, you can use the same regular expression approach as before and then join the matched words with a space. Here's how you can achieve this:

const inputString = "CameraReturnImageSaveLocation"; // Use regular expression with the 'g' (global) flag to find all words that start with a capital letter const regex = /\b[A-Z][A-Za-z]*\b/g; const matches = inputString.match(regex); // Join the matched words with a space const concatenatedSentence = matches.join(' '); console.log(concatenatedSentence); // Output: "Camera Return Image Save Location"


In this example, after finding the words that start with a capital letter using the regular expression, the join() function is used to concatenate the matched words with one space in between. The resulting sentence will be "Camera Return Image Save Location".

No comments:

Post a Comment