Reputation: 23
I have a string that I need to clean up using javascript.
Here below an example of the string that is giving me hell. I need to replace all the occurrences of '
that are not separated by a comma (like "apostrophe's" in my example).
Here's the example:
var paItems = [
[
38.20739,
-85.76427,
1,
'asd',
'314 Iowa Ave, Louisville, KY',
'this is something with apostrophe's',
'2000',
'2',
'2',
'1',
'Yes',
'',
'',
'Area Tennis|Satellite Dish|Controlled Access',
'asd',
'asd',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
''
],
[
38.20681,
-85.71437,
2,
'somewhere',
'3634 Poplar Level Rd, KY ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard ever since the 1500s, ',
'1200',
'3',
'2',
'1',
'Yes',
'Garage',
'2250',
'Private Pool|Area Pool|Satellite Dish',
'2',
'Some subdivision here',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
'Glenda C.'
],
];
What is the best way to approach it? I've tried regex but can't get it right.
Upvotes: 2
Views: 240
Reputation: 5395
I think you can use:
(\w)'(\w)
to match apostrophes surrounded by letters, and then replace for example e's
with es
, using captured groups($1$2
), like in:
var str = "'this is something with apostrophe's'";
var res = str.replace(/(\w)'(\w)/g, "$1$2");
with result:
'this is something with apostrophes'
Upvotes: 1