skip to Main Content

I am trying to convert/differentiate the input, but it is not working.

this is input – "on time" "flight" and I need to create an output like this – ""ON TIME"FLIGHT"

The Algo is whenever you find a phrase in double quotes you must convert that as ""phrase
and single word should be "word".

code :

const _searchKeyword = req.body.fullTextKeyword;
 const keywords = _searchKeyword.split(' ');

 
const formattedKeywords = keywords.map(keyword => {
  if (keyword.startsWith('"') && keyword.endsWith('"')) {
    // Phrase within double quotes
    const phrase = keyword.slice(1, -1);
    return `\"${phrase}\"`;
  } else {
    // Single word
    return keyword;
  }
});

// Join the escaped words with a space.
let concatenatedKeywords = formattedKeywords.join(" ");

but the above code gave me output like this – ""on time" "flight""

How Will I create output like this, any help is much appreciated!!!

2

Answers


  1. const _searchKeyword = 'on time "flight"';
    const keywords = _searchKeyword.split(' ');
    
    const formattedKeywords = keywords.map(keyword => {
      if (keyword.startsWith('"') && keyword.endsWith('"')) {
        // Phrase within double quotes
        const phrase = keyword.slice(1, -1);
        return `""${phrase}"`;
      } else {
        // Single word
        return `"${keyword}"`;
      }
    });
    
    // Join the formatted words with a space.
    const concatenatedKeywords = formattedKeywords.join(' ');
    
    console.log(concatenatedKeywords); // ""on time" "flight""
    
    
    1. For phrases within double quotes, I used "" instead of " to
      enclose the phrase.
    2. For single words, I added quotes " around the keyword.
    Login or Signup to reply.
  2. Usually you should make/use some string parser here. A simple parser would be in the most cases a finite-state machine:
    https://en.wikipedia.org/wiki/Finite-state_machine
    In our case we could operate in 2 modes: word or phrase (inside quotes):

    const myStr = '    "hello guys", some     words with "quotes inside"" some spaces inside " please keep quoted words as one "phrase / word" end-of-line ';
    
    // has a non closed quote
    const myWrongStr = '"hello guys", some words" with "quotes inside" please keep quoted words as one "phrase / word" ';
    
    console.log(myStr+ ':');
    console.log(splitToWordsWithQuotes(myStr));
    console.log('n'+myWrongStr+':');
    console.log(splitToWordsWithQuotes(myWrongStr));
    
    function splitToWordsWithQuotes(str) {
    
        let mode;
        const words = [];
        let word = '';
    
        const completeWord = () => (word && words.push(word)) + (word = '');
    
        for (let i = 0; i < str.length; i++) {
    
            const c = str[i];
    
            if (!mode) {
                if (c === ' ') {
                    continue;
                }
                if (c === '"') {
                    mode = 'phrase';
                } else {
                    word += c;
                    mode = 'word';
                }
                continue;
            }
    
            if (c === '"') {
                completeWord();
                mode = mode === 'word' ? 'phrase' : 'word';
                continue;
            }
    
            if (c === ' ') {
                if (mode === 'phrase') {
                    word += ' ';
                    continue;
                }
                completeWord();
                continue;
            }
    
            word += c;
    
        }
    
        completeWord();
    
        return words;
    
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search