最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RegEx for matching two single quotes from start and end of strings - Stack Overflow

programmeradmin4浏览0评论

I need to replace two quotes at the beginning and end of elements with one quote.

My code is:

const regex = /['']/g;
let categories = [];
let categoryArray = data.categories.split(','); // ''modiles2'', ''Men''s Apparel'', ''Women''s Apparel'', ''Jeans'', ''Sneakers''
    for (let value of Object.values(categoryArray)) {
        categories.push(categoryArray[i].replace(regex, '\''));
    }
}
return categories 
// What should be 'modiles2','Men''s Apparel','Women''s Apparel','Jeans','Sneakers'
// What es back ''modiles2'',''Men''s Apparel'',''Women''s Apparel'',''Jeans'',''Sneakers''

My regular expression replaces only the first of two quotation marks with one quotation mark.

How do I solve this problem?

I need to replace two quotes at the beginning and end of elements with one quote.

My code is:

const regex = /['']/g;
let categories = [];
let categoryArray = data.categories.split(','); // ''modiles2'', ''Men''s Apparel'', ''Women''s Apparel'', ''Jeans'', ''Sneakers''
    for (let value of Object.values(categoryArray)) {
        categories.push(categoryArray[i].replace(regex, '\''));
    }
}
return categories 
// What should be 'modiles2','Men''s Apparel','Women''s Apparel','Jeans','Sneakers'
// What es back ''modiles2'',''Men''s Apparel'',''Women''s Apparel'',''Jeans'',''Sneakers''

My regular expression replaces only the first of two quotation marks with one quotation mark.

How do I solve this problem?

Share Improve this question edited Apr 25, 2019 at 5:41 MegaRoks asked Apr 25, 2019 at 4:59 MegaRoksMegaRoks 9482 gold badges13 silver badges35 bronze badges 3
  • 1 const regex = /['']/g; check out what character classes are. – Rafael Commented Apr 25, 2019 at 5:02
  • @Rafael If I use '\' \ '', it replaces only at the beginning, I need both at the beginning and at the end, in the middle I don’t need to touch – MegaRoks Commented Apr 25, 2019 at 5:07
  • Do all elements in the array have ''' at the start and end? Because then you could just slice each value: elements.map(str => str.slice(1, -1). – Felix Kling Commented Apr 25, 2019 at 5:27
Add a ment  | 

5 Answers 5

Reset to default 2

You need to match two quotation marks - use this regex:

const regex = /^'.*'$/g;

Character classes look for any single character between the brackets. Character classes that have duplicate characters, like [''], can do without such duplication.

"''modiles2'',''Men''s Apparel'',''Women''s Apparel'',''Jeans'',''Sneakers''"
    .split(',')
    .forEach(s => console.log( s.replace(/^''|''$/g, '\'') ))

This RegEx might help to do so. You might consider using \x27 instead of ' just to be safe.

^(\x27{2,})(.+)(\x27{2,})$

This RegEx creates three groups, just to be simple, and you might use $2, which is your target output.

Your pattern uses a character class [''] which matches any of the listed which could be written as ['] which is just '

If there can be more than 2 at the start or at the end, you could also use a quantifier {2,} to match 2 or more single quotes and replace with a single quote. To match exactly 2 single quotes use ''.

^'{2,}|'{2,}$
  • ^'{2,} Match 2 or more quotes at the start of the string
  • | Or
  • '{2,}$ Match 2 or more single quotes at the end of the string

Regex demo

Use the /g global flag to replace all occurrences.

console.log("''modiles2'',''Men''s Apparel'',''Women''s Apparel'',''Jeans'',''Sneakers''"
.split(',')
.map(s => s.replace(/^'{2,}|'{2,}$/g, "'")))

Here is my two cents, in one line:

return data.categories.split(',').map(val => val.trim().replace(/^'{2,}|'{2,}$/, "'"));
发布评论

评论列表(0)

  1. 暂无评论