[英]Reverse words in array string matching punctuation in Javascript
How do I reverse the words in this string including the punctuation?如何反转此字符串中的单词,包括标点符号?
String.prototype.reverse = function () {
return this.split('').reverse().join('');
}
var str = "This is fun, hopefully.";
str.reverse();
Currently I am getting this:目前我得到这个:
".yllufepoh ,nuf si sihT"
When I want to return this:当我想返回这个时:
"sihT si nuf, yllufepoh."
You could reverse each word instead of the whole string, but you have to keep spaces, periods etc seperate, so a word boundary is needed您可以反转每个单词而不是整个字符串,但是您必须将空格、句点等分开,因此需要一个单词边界
String.prototype.reverse = function () { return this.split(/\\b/g).map(function(word) { return word.split('').reverse().join(''); }).join(''); } var str = "This is fun, hopefully."; document.body.innerHTML = str.reverse();
Note that this moves the comma one space as it gets the comma and the space in one boundary and swaps them.请注意,这会将逗号移动一个空格,因为它会在一个边界中获取逗号和空格并交换它们。 If the comma needs to stay in the same spot, split on spaces as well, and change the regex to
/(\\b|\\s)/g
如果逗号需要保持在同一位置,也可以在空格上拆分,并将正则表达式更改为
/(\\b|\\s)/g
Simply reversing the string wont give the solution.简单地反转字符串不会给出解决方案。
var str = "This is fun, hopefully."; alert(str.split("").reverse().join("").split(" ").reverse().join(" "));
You can imagine that you receive a stream of letters and you have to construct words based on some separators (like: spaces, commas, dashes .etc).你可以想象你收到了一个字母流,你必须根据一些分隔符(如:空格、逗号、破折号等)构造单词。
While reading each character you keep constructing the word in reverse.在阅读每个字符时,您一直在反向构造单词。
When you hit any separator you finished the word.当你点击任何分隔符时,你就完成了这个词。
Now you just add it to the result and append the separator (this way the separators will not be put at the beginning of the word, but at the end).现在您只需将它添加到结果中并附加分隔符(这样分隔符不会放在单词的开头,而是放在结尾)。
Here is an example:下面是一个例子:
const inputString = "HELLO, Welcome to Google's meeting. My name is Jean-Piere... Bye"; console.log('Normal words: ', inputString); const result = reverseWords(inputString); console.log('Words reversed: ', result); function reverseWords(str='', separators=' ,.-') { let result = ''; let word = ''; for (const char of str) { if (separators.includes(char)) { result += word + char; word = ''; } else { word = char + word; } } // Adds last remaining word, if there is no separator at the end. result += word; return result; }
You can reverse each word in a string in squence by splitting that word in to an array of words and then reversing each word and storing it in a new array and then joining that array as shown below.您可以按顺序反转字符串中的每个单词,方法是将该单词拆分为一个单词数组,然后反转每个单词并将其存储在一个新数组中,然后加入该数组,如下所示。
//1) Reverse words function reverseWords(str) { // Go for it let reversed; let newArray=[]; reversed = str.split(" "); for(var i = 0;i<reversed.length; i++) { newArray.push(reversed[i].split("").reverse().join("")); } return newArray.join(" "); } let reversedString = reverseWords("This is fun, hopefully."); console.log("This is the reversed string : ",reversedString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.