4

Yeah, I'm trying to remove everything except the capital letters, though it doesn't really seem to go well.

I used the following code,

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

It doesn't seem to work properly, while it does work using PHP.

kukkuz
  • 41,512
  • 6
  • 59
  • 95
Martijn Ebbens
  • 253
  • 3
  • 15
  • 1
    Note: PHP and JavaScript have different flavors of regex, both of which are expansions on the [formal definition of regex](https://en.wikipedia.org/wiki/Regular_expression), so it isn't necessarily a safe assumption that one language's regex will transfer to another... not to say that this example isn't an overlap between the two – Patrick Barr Aug 02 '17 at 15:55

2 Answers2

6

Add the global option at the end of the regex - see demo below:

console.log("AkjkljKK".replace(/(?![A-Z])./g, ''));
kukkuz
  • 41,512
  • 6
  • 59
  • 95
4

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

var str = "sOmeVALUE";

console.log(str.replace(/[^A-Z]/g, ""));
Dij
  • 9,761
  • 4
  • 18
  • 35