Removing everything except capital letters, with replacement

Yes, I am trying to delete everything except the capital letters, although in reality it does not look good.

I used the following code,

String.replace(/(?![A-Z])./, '');

It seems to work incorrectly, although it works using PHP.

+4
source share
2 answers

Add an option globalat the end regex- see demo below:

console.log("AkjkljKK".replace(/(?![A-Z])./g, ''));
Run codeHide result
+6
source

you can use [^A-Z]to remove everything except capital letters. Also use gto replace all occurrences, not just the first one.

var str = "sOmeVALUE";

console.log(str.replace(/[^A-Z]/g, ""));
Run codeHide result
+4
source

Source: https://habr.com/ru/post/1682874/


All Articles