Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

javascript - JS - Use replace to replace space period with period

Using replace, how do you replace space period with just period anywhere within text?

Currently I could only figure out searching for space and replacing it with a period, but I need for it to look for a pattern of space + period. BTW, this pattern is random, so some sentences appear correctly, without the excess space before the period.

Example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit . Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Vitae elementum curabitur .

So far I have the following, but this is wrong as it replaces all spaces with periods.

cleanData(str) {
  return str.replace(/s/g, '.');
}
question from:https://stackoverflow.com/questions/65861822/js-use-replace-to-replace-space-period-with-period

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm a big fan of regex, but I don't think it's needed in this case. Maybe you could try something like this:

cleanData(str) {
   return str.replaceAll(' .', '.');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...