我已经trying to convert/differentiate the input岁了,但它不起作用.

这是input-"准点""飞行",我需要创建一个这样的output-""准点"飞行"

The 算法 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(" ");

但以上code gave me output like this-"准时"\"航班\"

我将如何创建这样的输出,任何帮助都非常感谢!

推荐答案

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. 对于双引号内的短语,我使用""而不是 请将该短语括起来.
  2. 对于单字,我在关键字两边加了引号.

Javascript相关问答推荐

如何使用3个部件之间的路由?

vue3类型脚本中可能未定义对象''

调用SEARCH函数后,程序不会结束

如何使用Paged.js仅渲染特定页面

Flisk和JS错误:未捕获的Syntax错误:意外的令牌'<'

我的glb文件没有加载到我的three.js文件中

我试图实现用户验证的reduxstore 和操作中出了什么问题?

单击子元素时关闭父元素(JS)

django无法解析余数:[0] from carray[0]'

函数返回与输入对象具有相同键的对象

JSDoc创建并从另一个文件导入类型

使用领域Web SDK的Vite+Vue应用程序中的Web程序集(WASM)错误

更改预请求脚本中重用的JSON主体变量- Postman

扩展类型的联合被解析为基类型

在使用REACT更改了CSS类之后,无法更改CSS样式

Reaction-SWR-无更新组件

如果没有页面重新加载Angular ,innerHTML属性绑定不会更新

在JS/TS中将复杂图形转换为数组或其他数据 struct

ngOnChanges仅在第二次调用时才触发

postman 预请求中的hmac/sha256内标识-从js示例转换