简体   繁体   English

正则表达式选择双引号之间的特定字符

[英]Regex select specific characters between double quotes

I would like to extract specific character ie ;我想提取特定的字符,即; that are located between quotes ( " ) with a Regex expression.用正则表达式位于引号 ( " ) 之间。


String example :字符串示例:

Lorem;ipsum;"dolor;sit;";amet; Lorem;ipsum;"dolor;sit;";amet;


Should select every ;应该选择每一个; in quotes :在引号中:

Lorem;ipsum;"dolor ; sit ; ";amet; Lorem;ipsum;"dolor ;; ";amet;


I tried this one but it doesn't work我试过这个,但它不起作用

(?<=\")(;)*(?=\")

Any idea ?任何的想法 ? Thank you in advance先感谢您

You will have to do it in two steps:您必须分两步完成:

  1. select every parts between quotes: /"[^"]+"/gm选择引号之间的每个部分: /"[^"]+"/gm
  2. in these matchs, search for ;在这些匹配中,搜索;

you should be able to use String.prototype.replace with the given regex and look for ";"您应该能够将String.prototype.replace与给定的正则表达式一起使用并查找“;” in your replace callback.在您的替换回调中。

here is a demo:这是一个演示:

 function escapeCsvDelimiter(input) { return input.replace(/"[^"]+"/gm, (match) => match.replace(/;/g, '\\\\;')); } const test = 'Lorem;ipsum;"dolor;sit;";amet;"jhv;"'; const result = escapeCsvDelimiter(test); console.log(result);

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

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