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
enclose the phrase.
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
orphrase
(inside quotes):