简体   繁体   中英

RegEx Data Values Javascript white Space

I am trying to add the correct white space for data i am receiving. currently it shows like this

NotStarted

ReadyforPPPDReview

this is the code i am using

.replace(/([A-Z])/g, '$1')

"NotStarted" shows correct "Not Started" but "ReadyforPPPDReview" shows "Readyfor PPPD Review" when it should look like this "Ready for PPPD Review"

what is the best way to handle both of these using one regex or function?

You would need an NLP engine to handle this properly. Here are two approaches with simple regex, both have limitations:

1. Use list of stop words

We blindly add spaces before and after the stop words:

 var str = 'NotStarted, ReadyforPPPDReview'; var wordList = 'and, for, in, on, not, review, the'; // stop words var wordListRe = new RegExp('(' + wordList.replace(/, */g, '|') + ')', 'gi'); var result1 = str.replace(wordListRe, ' $1 ') // add space before and after stop words.replace(/([az])([AZ])/g, '$1 $2') // add space between lower case and upper case chars.replace(/ +/g, ' ') // remove excessive spaces.trim(); // remove spaces at start and end console.log('str: ' + str); console.log('result1: ' + result1);

As you can imagine the stop words approach has some severe limitations. For example, words formula input would result in for mula in put .

1. Use a mapping table

The mapping table lists words that need to be spaced out (no drugs involved), as in this code snippet:

 var str = 'NotStarted, ReadyforPPPDReview'; var spaceWordMap = { NotStarted: 'Not Started', Readyfor: 'Ready for', PPPDReview: 'PPPD Review' // add more as needed }; var spaceWordMapRe = new RegExp('(' + Object.keys(spaceWordMap).join('|') + ')', 'gi'); var result2 = str.replace(spaceWordMapRe, function(m, p1) { // m: matched snippet, p1: first group return spaceWordMap[p1] // replace key in spaceWordMap with its value }).replace(/([az])([AZ])/g, '$1 $2') // add space between lower case and upper case chars.replace(/ +/g, ' ') // remove excessive spaces.trim(); // remove spaces at start and end console.log('str: ' + str); console.log('result2: ' + result2);

This approach is suitable if you have a deterministic list of words as input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM