简体   繁体   中英

Return a word from a string

I have a string of 3 words 'walking running swimmming' and I want to return only those who have double letters. Well, I found a solution how to do this, but I'm stuck on separating the words. Any ideas? I've tried .map (returns 'not a function'), .split(). Maybe I'm doing something wrong?

P.S. var repeats = /([a-zA-Z])\1/;

You can just run .split(" ") on your string and then iterate through the list to perform your check

help if you need it

Split the words into an array by the space. Use .filter to return only those that have a repeated letter. Inside the filter, test if it has a repeated letter with regex and .test . Returns true if it does, adding it to the new array.

function doubles(words) {
    const arr = words.split(" ");
    return arr.filter(word => (/([a-zA-Z]).*?\1/).test(word));
}

doubles("walking running swimming");  // returns ["running", "swimming"]

Regex for the test was found here

I think what you want it something like this:

 let result = 'walking running swimmming' .split(' ') // turn into array .filter(s => /([a-zA-Z])\\1/.test(s)) // filter out elements of array console.log(result) 

returns

["running", "swimmming"]

There might be a more performance efficient answer, but I'd probably go for this one because it's minimalist.

'walking running swimming'
  .split(' ')
  .filter(word => /(.)\1/.test(word));

Explanations

String.prototype.split returns an array of words.

Array.prototype.filter calls a lambda function for each element of the array. If the function returns true, that element is kept, else it is discarded. The filtered array is then returned.

RegExp.prototype.test returns true if the string matches the regex.

word => ... is a short notation for function (word) { return ... }

/(.)\\1/ is a RegExp which matches strings that contain any character twice in a row. You could use a-zA-Z instead of . if you prefer, it will have different results if your strings might contain other characters.


So with that sample string, this is what happens:

  1. 'walking running swimming' becomes ['walking', 'running', 'swimming']
  2. For each element of the array, the word => ... lambda is called.
    • walking doesn't have a sequence of two repeated characters, so test will return false.
    • running and swimming will test true.
  3. filter returns an array of words that tested true, ['running', 'swimming']

You can get multiple matches from a string ( \\w matches [a-zA-Z0-9_] ) :

 console.log( 'walking running swimmming'.match(/\\w*(\\w)\\1\\w*/g) ) console.log( 'walking running swimmming'.match(/[az]*([az])\\1[az]*/gi) ) 

To match words with only 2 repeating characters:

 console.log( 'walking running swimmming'.replace(/(.)\\1\\1+/g, 0).match(/\\w*(\\w)\\1\\w*/g) ) 

Try the following:

 var str = 'walking running swimmming'; var strArr = str.split(' '); strArr.forEach(function(word){ if(word.match(/([A-Za-z])\\1/)){ console.log(word); } }); 

 var words = "walking running swimming".split(' ') // ["walking", "running", "swimming"] var double_letter_words = words.filter( function(w) { for (i = 0; i < w.length - 1; i++) { if (w[i] == w[i+1]) return true }; return false; }) // ["running", "swimming"] console.log(double_letter_words); 

I should note that while I didn't use your regular expression (like other answers), it does correctly filter the words and has slightly better performance than the other answers, and also is somewhat easier to maintain and modify than a regex.

https://jsperf.com/return-double-letter-words-from-string

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