本文介绍了在两个单词之间添加一个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一些词,例如浅紫色"和深红色",分别存储为浅紫色"和深红色".如何检查单词"LightPurple"中的大写字母,并在单词"Light"和"Purple"之间放置一个空格以形成单词"Light Purple".

I have some words like "Light Purple" and "Dark Red" which are stored as "LightPurple" and "DarkRed". How do I check for the uppercase letters in the word like "LightPurple" and put a space in between the words "Light" and "Purple" to form the word "Light Purple".

提前感谢您的帮助

推荐答案

您可以使用正则表达式在大写字母旁边的小写字母处添加空格.

You can use a regex to add a space wherever there is a lowercase letter next to an uppercase one.

类似这样的东西:

"LightPurple".replace(/([a-z])([A-Z])/, '$1 $2')

更新:如果您有2个以上的单词,则需要使用 g 标志将其全部匹配.

UPDATE: If you have more than 2 words, then you'll need to use the g flag, to match them all.

"LightPurpleCar".replace(/([a-z])([A-Z])/g, '$1 $2')

更新2 :如果试图拆分 CSVFile 之类的单词,则可能需要使用此正则表达式:

UPDATE 2: If are trying to split words like CSVFile, then you might need to use this regex instead:

"CSVFilesAreCool".replace(/([a-zA-Z])([A-Z])([a-z])/g, '$1 $2$3')

这篇关于在两个单词之间添加一个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:00