简体   繁体   English

双引号之间的 JavaScript 文本

[英]JavaScript text between double quotes

I would like to get the text between double quotes using JavaScript.我想使用 JavaScript 获取双引号之间的文本。 I found online something like title.match(/".*?"/);我在网上找到类似title.match(/".*?"/); but the thing is that sometimes I have text between double quotes but sometimes there are no quotes.但问题是有时我在双引号之间有文本,但有时没有引号。 What I am saying is that sometimes I receive strings like: Neque porro quisquam est qui dolorem ipsum and sometimes strings like: Neque "porro quisquam est" qui dolorem ipsum .我的意思是,有时我会收到这样的字符串: Neque porro quisquam est qui dolorem ipsum有时会收到这样的字符串: Neque "porro quisquam est" qui dolorem ipsum The thing is, when I have text containing double quotes I want to retrieve the text between them but when they aren't present, I'd like the whole text.问题是,当我有包含双引号的文本时,我想检索它们之间的文本,但是当它们不存在时,我想要整个文本。 Also I have observered that string.indexOf("\\"") does not work and I don't really know how to approach this problem. Thanks.我也观察到string.indexOf("\\"")不起作用,我真的不知道如何解决这个问题。谢谢。

Try:尝试:

let str1 = 'Neque porro quisquam est qui dolorem ipsum';
let str2 = 'Neque "porro quisquam est" qui dolorem ipsum';
let str3 = 'Neque "porro';
let str4 = 'Neque "porro" quisquam "est" qui dolorem ipsum';

function extractFirstText(str){
  const matches = str.match(/"(.*?)"/);
  return matches
    ? matches[1]
    : str;
}


function extractAllText(str){
  const re = /"(.*?)"/g;
  const result = [];
  let current;
  while (current = re.exec(str)) {
    result.push(current.pop());
  }
  return result.length > 0
    ? result
    : [str];
}

Then然后

extractFirstText(str1);
//Neque porro quisquam est qui dolorem ipsum

extractFirstText(str2);
//porro quisquam est

extractFirstText(str3);
//Neque "porro

extractFirstText(str4);
//porro

extractAllText(str1);
//Array [ "Neque porro quisquam est qui dolorem ipsum" ]

extractAllText(str2);
//Array [ "porro quisquam est" ]

extractAllText(str3);
//Array [ "Neque \"porro" ]

extractAllText(str4);
//Array [ "porro", "est" ]

EDIT reworked to take into account both @AshishMaity comment in a discarded edit about matching more than one substring, and @JosephCho comment about the original breaking in case there is a single quote (str3 in the case above)编辑重新设计以考虑@AshishMaity 在有关匹配多个子字符串的丢弃编辑中的评论,以及@JosephCho 关于原始中断的评论,以防有单引号(在上述情况下为 str3)

try it with this one:试试这个:

/"((?:\\.|[^"\\])*)"/

正则表达式可视化

Debuggex Demo调试器演示

In a single regex:在单个正则表达式中:

var m = s.match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");

TEST:测试:

s = 'Neque "porro quisquam est" qui dolorem ipsum';
m = s.match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");
//=> porro quisquam est

s = 'Neque porro quisquam est qui dolorem ipsum';
m = s.match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");
//=> Neque porro quisquam est qui dolorem ipsum

split() function can be used get value in double quote split() 函数可用于获取双引号中的值

 let str1 = 'Neque "porro quisquam est" qui dolorem ipsum'; let str2 = 'Neque porro quisquam est qui dolorem ipsum'; function findFirstOccurance(str){ const matches = str.split('"'); return matches[1] ? matches[1] : str; } console.log(findFirstOccurance(str1)); console.log(findFirstOccurance(str2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM