最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing everything except capital letters using replace - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Aug 2, 2017 at 15:51 kukkuz 42.4k6 gold badges64 silver badges102 bronze badges asked Aug 2, 2017 at 15:48 Martijn EbbensMartijn Ebbens 2533 silver badges15 bronze badges 1
  • 1 Note: PHP and JavaScript have different flavors of regex, both of which are expansions on the formal definition of regex, 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 Commented Aug 2, 2017 at 15:55
Add a ment  | 

2 Answers 2

Reset to default 6

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

console.log("AkjkljKK".replace(/(?![A-Z])./g, ''));

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, ""));

发布评论

评论列表(0)

  1. 暂无评论