简体   繁体   English

反转数组字符串中的单词匹配 Javascript 中的标点符号

[英]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.简单地反转字符串不会给出解决方案。

  1. Get each word.得到每一个字。
  2. Reverse It反转它
  3. Again rejoin再次加入

 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.

相关问题 将字符串拆分为JavaScript中的单词,标点符号和空格数组 - Split a string into an array of words, punctuation and spaces in JavaScript Javascript:删除字符串标点符号并拆分成单词? - Javascript: Remove string punctuation and split into words? 如何使用JavaScript将字符串解析为单词和标点符号 - How to parse string into words and punctuation marks using javascript JavaScript:如何在保持单词、空格和标点符号的原始顺序的同时反转每个单词中的字母? - JavaScript: How to reverse letters in each word while maintaining the original order of words, spaces and punctuation? 根据字符串中的标点将字符串拆分为数组元素-JavaScript - Split String Into Array Elements Based On Punctuation In String - JavaScript AngularsJS JavaScript匹配单词并添加字符串 - AngularsJS JavaScript Matching Words and Adding String 通过与javascript中数组的单词进行比较来连接字符串的单词 - concatenate words of a string by comparing with words of an array in javascript 用Java语言中的单词数组拆分字符串 - Splitting a String by an Array of Words in Javascript 如何在不反转标点的情况下反转字符串? - How to reverse a string in place without reversing the punctuation? Javascript通过匹配字符串对数组进行排序 - Javascript sort an array by matching to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM