Escape double quotes within double quotes with Regex[JS] - javascript

I have a JSON string: '{"place": {"address": "Main Street, \"The House\""}}'.
Before parsing it with JSON.parse() I have to make sure that the double quotes within double quotes are escaped properly.
I was trying to come up with the regex that would match "The House", but failed to so.
Any idea on how can I achieve desired result?

This would be possible with the help of positive lookahead assertion.
var s = '{"place": {"address": "Main Street, "The House""}}';
alert(s.replace(/"((?:"[^"]*"|[^"])*?)"(?=[:}])/g, function(m,group)
{
return '"' + group.replace(/"/g, '\\"') + '"'
}))
OR
var s = '{"place": {"address": "Main Street, "The House"", "country": "United Kingdom"}}';
alert(s.replace(/"((?:"[^"]*"|[^"])*?)"(?=[:},])(?=(?:"[^"]*"|[^"])*$)/gm, function(m,group)
{
return '"' + group.replace(/"/g, '\\"') + '"'
}))

Related

Split a string into an array of words, punctuation and spaces in JavaScript

I have a string which I'd like to split into items contained in an array as the following example:
var text = "I like grumpy cats. Do you?"
// to result in:
var wordArray = ["I", " ", "like", " ", "grumpy", " ", "cats", ".", " ", "Do", " ", "you", "?" ]
I've tried the following expression (and a similar varieties without success
var wordArray = text.split(/(\S+|\W)/)
//this disregards spaces and doesn't separate punctuation from words
In Ruby there's a Regex operator (\b) that splits at any word boundary preserving spaces and punctuation but I can't find a similar for Java Script. Would appreciate your help.
Use String#match method with regex /\w+|\s+|[^\s\w]+/g.
\w+ - for any word match
\s+ - for whitespace
[^\s\w]+ - for matching combination of anything other than whitespace and word character.
var text = "I like grumpy cats. Do you?";
console.log(
text.match(/\w+|\s+|[^\s\w]+/g)
)
Regex explanation here
FYI : If you just want to match single special char then you can use \W or . instead of [^\s\w]+.
The word boundary \b should work fine.
Example
"I like grumpy cats. Do you?".split(/\b/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ". ", "Do", " ", "you", "?"]
Edit
To handle the case of ., we can split it on [.\s] as well
Example
"I like grumpy cats. Do you?".split(/(?=[.\s]|\b)/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ".", " ", "Do", " ", "you", "?"]
(?=[.\s] Positive look ahead, splits just before . or \s
var text = "I like grumpy cats. Do you?"
var arr = text.split(/\s|\b/);
alert(arr);

RegEx pattern to always uppercase "CA" and "USA" in address string

end_address = 'joe place, 555 test street, sacramento, ca, usa 95814';
end_address = end_address.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
The final result of this is:
Joe Place, 555 Test Street, Sacramento, Ca, Usa 95814
but my desired output is:
Joe Place, 555 Test Street, Sacramento, CA, USA 95814
How can I match a string so that "CA" and "USA" are always uppercase like the desired output?
This will work:
end_address = 'jOe place, 555 test street, sacramento, ca, usa 95814'.toLowerCase();
end_address = end_address.replace(/\b(usa\b|ca\b|\w)/g, function(txt) { return txt.toUpperCase(); });
alert(end_address);
First, I lowercase it all, then apply the capitalization regex, /\b(usa\b|ca\b|\w)/g, which looks for the start of a word. It will match, then capitalize "usa", "ca" or the first character of the word.
Assuming the pattern will always be the same, you need to do a second pass at the string.
var result = end_address
.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}).replace(/(\w{2}), (\w+) (\d{5}(-\d{4})?)$/, function(match, state, country, zip) {
return state.toUpperCase() + ", " + country.toUpperCase() + ", " + zip;
});
What I'd do is something like this:
address = address.replace(
/([a-z]{2}),\s*([a-z]{3})\s*(\d{5})$/i,
function(match, state, country, zip) {
return state.toUpperCase() + ", " + country.toUpperCase() + " " + zip;
})
It'll do the replacement in one pass, and will only touch the state/country (assuming those are at the end of the string).
See the regex work on Regex101

JSON Remove trailing comma from last object

This JSON data is being dynamically inserted into a template I'm working on. I'm trying to remove the trailing comma from the list of objects.
The CMS I'm working in uses Velocity, which I'm not too familiar with yet. So I was looking to write a snippet of JavaScript that detects that trailing comma on the last object (ITEM2) and removes it. Is there a REGEX I can use to detect any comma before that closing bracket?
[
{
"ITEM1":{
"names":[
"nameA"
]
}
},
{
"ITEM2":{
"names":[
"nameB",
"nameC"
]
}
}, // need to remove this comma!
]
You need to find ,, after which there is no any new attribute, object or array.
New attribute could start either with quotes (" or ') or with any word-character (\w).
New object could start only with character {.
New array could start only with character [.
New attribute, object or array could be placed after a bunch of space-like symbols (\s).
So, the regex will be like this:
const regex = /\,(?!\s*?[\{\[\"\'\w])/g;
Use it like this:
// javascript
const json = input.replace(regex, ''); // remove all trailing commas (`input` variable holds the erroneous JSON)
const data = JSON.parse(json); // build a new JSON object based on correct string
Try the first regex.
Another approach is to find every ,, after which there is a closing bracket.
Closing brackets in this case are } and ].
Again, closing brackets might be placed after a bunch of space-like symbols (\s).
Hence the regexp:
const regex = /\,(?=\s*?[\}\]])/g;
Usage is the same.
Try the second regex.
For your specific example, you can do a simple search/replace like this:
,\n]$
Replacement string:
\n]
Working demo
Code
var re = /,\n]$/;
var str = '[ \n { \n "ITEM1":{ \n "names":[ \n "nameA"\n ]\n }\n },\n { \n "ITEM2":{ \n "names":[ \n "nameB",\n "nameC"\n ]\n }\n },\n]';
var subst = '\n]';
var result = str.replace(re, subst);
Consider the Json input = [{"ITEM1":{"names":["nameA"]}},{"ITEM2":{"names":["nameB","nameC"]}},] without whitespaces.
I suggest a simple way using substring.
input = input.substring(0, input.length-2);
input = input + "]";
I developped a simple but useful logic for this purpose - you can try this.
Integer Cnt = 5;
String StrInput = "[";
for(int i=1; i<Cnt; i++){
StrInput +=" {"
+ " \"ITEM"+i+"\":{ "
+ " \"names\":["
+ " \"nameA\""
+ "]"
+"}";
if(i ==(Cnt-1)) {
StrInput += "}";
} else {
StrInput += "},";
}
}
StrInput +="]";
System.out.println(StrInput);

Array to more than one string

I have a JavaScript array:
cities = ["LA", "NYC", "Riyadh", "Frankfurt"]
The cities.toString() function will give me
"LA, NYC, Riyadh, Frankfurt"
How do I get
"LA", "NYC", "Riyadh", "Frankfurt"
The simplest way I can think of is to use JSON.stringify and drop the first and last characters with slice, like this
var cities = ["LA", "NYC", "Riyadh", "Frankfurt"]
console.log(JSON.stringify(cities).slice(1, -1));
// "LA","NYC","Riyadh","Frankfurt"
If you want it, exactly like you mentioned in your answer, use map function to generate new Strings surrounded by double quotes and finally join them with , like this
console.log(cities.map(function (currentCity) {
return '"' + currentCity + '"';
}).join(', '));
// "LA", "NYC", "Riyadh", "Frankfurt"
If your actual strings will not have any , in them, then you can chain the split and join calls, like this
console.log(JSON.stringify(cities).slice(1, -1).split(",").join(", "));
// "LA", "NYC", "Riyadh", "Frankfurt"
You can use join function of array to join the array elements using any separator like:
var result = '"' + cities.join('", "') + '"' ;
Check the working demo fiddle
If you want to change an array into a string, there's array.join. It works well with whatever delimiter you want to use:
var array = ["LA", "NYC", "Riyadh", "Frankfurt"];
var string = '"' + array.join('", "') + '"';
// Result: "LA", "NYC", "Riyadh", "Frankfurt"

Regex: Match all double quotes in string and add comma

I need to turn a string like this:
' query: "help me" distance: "25" count: "50" '
into a javascript object or json string that looks like this:
'{ query: "help me", distance: "25", count: "50" }'
Something like this, perhaps:
var query = ' query: "help me" distance: "25" count: "50"';
query = '{' + query.replace(/"(?=\s)/g, '",') + '}';
console.log(query);
With that lookahead expression I just put comma after all the double quotation marks that are followed by whitespace symbol.
Having said that, I'd strongly suggest reconsidering the method of constructing your params: somehow I feel you could get away with simple JSON.stringify-ing the params. It will be much more bulletproof - and easier to parse too.

Categories

Resources