I have this string
product_id:WDC WD2500YS-18S revision:6C07 size(GB):232 state: ONLINE
and I need convert to anything else, like json:
{
"product_id": "WDC WD2500YS-18S",
"revision": "6C07",
"size(GB)": "232",
"state": "ONLINE"
}
I tried this /([^:]+)/g, but is not working because I need that the WD2500YS-18S be togetter with WDC WD2500YS-18S.
You must use the fact that the key doesn't contains spaces and check what follows with a lookahead.
/([^\s:]+):\s*([^:]+?)(?=\s+[^\s:]+:|\s*$)/g
This solution doesn't rely on lookaheads or lookbehinds, and utilizes the browser's built-in json parser to verify that the output is valid. It leverages the fact that the keys do not contain spaces. So ([^ ]+): will find a key. We just wrap the key and value with quotation marks and let the built-in json parser do the rest.
var input = 'product_id:WDC WD2500YS-18S revision:6C07 size(GB):232 state: ONLINE';
console.log(JSON.parse(
'{' +
input.replace(/(^| )([^ ]+):/g,'","$2":"').substring(2) +
'"}'
));
outputs:
{
product_id: "WDC WD2500YS-18S",
revision: "6C07",
size(GB): "232",
state: " ONLINE"
}
Try /([^:]+):\s?([^:]+)(?:\s|$)/g - regex101
Related
I'm trying to create a command parser for a Discord bot for when it receives a message, but I am having issues with dealing with nested quotes. I have made it so that it can parse a string with double quotes and flags, but it does not handle nested quotes.
Here are my requirements:
Handle double quotes.
Handle nested double quotes.
Handle flags (can be anywhere after !command).
A flag without a specified value defaults to a value of true/1.
For example, the following string:
!command that --can "handle double" quotes "and \"nested double\" quotes" --as --well=as --flags="with values"
...should result in the following arguments: command, that, handle double, quotes, and "nested double" quotes and the following flags: "can": true, "as": true, "well": "as", "flags": "with values".
Here is what I have so far:
// splits up the string into separate arguments and flags
const parts = content.slice(1).trim().match(/(--\w+=)?"[^"]*"|[^ "]+/g)
.map(arg => arg.replace(/^"(.*)"$/, '$1'));
// separates the arguments and flags
const [ args, flags ] = parts.reduce((parts, part) => {
// check if flag or argument
if (part.startsWith('--')) {
// check if has a specified value or not
if (part.includes('=')) {
// parses the specified value
part = part.split('=');
const value = part.slice(1)[0];
parts[1][part[0].slice(2)] = value.replace(/^"(.*)"$/, '$1');
} else {
parts[1][part.slice(2)] = true;
}
} else {
parts[0].push(part);
}
return parts;
}, [[], {}]);
This currently parses into the following arguments: command, that, handle double, quotes, and \, nested, double\, quotes and the following flags: "can": true, "as": true, "well": "as", "flags": "with values".
I modified the first RegEx to allow \" in the middle of quoted values. The following line:
const parts = content.slice(1).trim().match(/(--\w+=)?"[^"]*"|[^ "]+/g)
...changed to:
const parts = content.slice(1).trim().match(/(--\S+=)?"(\\"|[^"])*"|[^ "]+/g)
Modifications
The "[^"]*" section was changed to "(\\"|[^"])*" to allow \" to validate, preventing the quoted value from being terminated by quotes with backslashes before them.
I changed the \w in (--\w+=)? to a \S resulting in (--\S+=)? to allow more letters to validate.
I have a string:
for (;;); {
"__ar": 1,
"payload": null,
"jsmods": {
"require": [
["ServerRedirect", "redirectPageTo", [],
["https:\/\/bigzipfiles.facebook.com\/p\/dl\/download\/file.php?r=100028316830939&t=100028316830939&j=11&i=5823694&ext=12121516&hash=AaBVNURld6wrKBcU", true, false]
]
],
"define": [
["KSConfig", []
}
I try to regex this to be:
https://bigzipfiles.facebook.com/p/dl/download/file.php?r=100028316830939&t=100028316830939&j=11&i=5823694&ext=12121516&hash=AaBVNURld6wrKBcU
I've used
var results = $(document).find("pre").html();
var regex1 = new RegExp(/["\w\.\/\;\?\=\-\&\\\\"]/);
var resultsReplace = regex1.exec(results);
console.log(resultsReplace);
but it is not working.
Can anyone help me, please?
Assuming it is always URL and the delimiter is always " we can use a much simpler regex here, like:
str.match(/http[^"]+/)[0]
So we looking for a string starting with http with any following characters except " since the match string never can get one because this is a delimiter.
LMK if cases are wider (not an url, may not start with http, etc.) and I'll adjust regex.
I have a invalid json string like this:
[{
"itemID": "1",
"itemTitle": "Mango",
"itemText": "some text here"again text",
"ThumbUrl": "http://someurl.com/mango.jpg",
"itemContent": null
}, {
"itemID": "2",
"itemTitle": "orange",
"itemText": "someother text " again another texther",
"ThumbUrl": "http://someurl.com/orange.jpg",
"itemContent": null
}
]
javascript:
$.get("http://www.someapiurl.com/getdata.php", function(data, status){
//here i want to replace json key value to empty before parsing the json
var json = $.parseJSON(data);
}
I want to change the value of itemText to word empty using regular expression. Could any one help me how to achieve this using regular expression ?Thanks
Note:
-JSON is invalid (That is the way i recive it so i have to correct it before parsing it
-The Json response some time got double quotes in itemText)
-the itemText key value is across multiple lines(mix of unicode and non unicode)and long(not on online line)
Edit: I have used this php regular expression to achive same thing. Could you guys help me convert it to javascript regular expresion?
print_r(preg_replace('/\"itemText\"\:\".*?\"\,/s', '"textTitle$1":"empty",',$json));
Edit2:
Finally this replaced itemText with word empty for me in all cases:
data.replace(/("itemText"\s*:\s*")[\s\S]*?ThumbUrl/g, '$1empty","ThumbUrl')
The right approach is to ask your data provider to fix the issue on their side.
As a temporary workaround, you may use
.replace(/("itemText[12]"\s*:\s*")[\s\S]*?",/g, '$1empty"')
See the regex demo
var regex = /("itemText[12]"\s*:\s*")[\s\S]*?",/g;
var str = `[{
"itemID": "1",
"itemTitle": "Mango",
"itemText1": "some text here"again text",
"ThumbUrl": "http://someurl.com/mango.jpg",
"itemContent": null
}, {
"itemID": "2",
"itemTitle": "orange",
"itemText2": "someother text " again another texther",
"ThumbUrl": "http://someurl.com/orange.jpg",
"itemContent": null
}
]`;
var subst = '$1empty"';
var result = str.replace(regex, subst);
console.log(result);
Details:
("itemText[12]"\s*:\s*") - Group 1 capturing
"itemText - literal text "itemText,
[12] - 1 or 2
" - a double quote
\s*:\s* - 0+ whitespaces, :, and again 0+ whitespaces
[\s\S]*? - any 0+ chars as few as possible up to the first
", - a double quote and a comma.
Finally this replaced itemText with word empty for me in all cases:
data.replace(/("itemText"\s*:\s*")[\s\S]*?ThumbUrl/g, '$1empty","ThumbUrl')
I need to check if a string represents a valid namespace format. A namespace is comprised of ids separated with dots. Each id starts with an alphabetic character and continues with an alphanumeric character.
Valid namespaces:
"com.company.package"
"com.company"
"com"
Invalid namespaces:
"1com.company.package"
"com.1company"
"com.com%any"
".com.company"
"com.company."
"com "
" com"
""
"."
"com..company"
Currently I use this simple regexp but it really don't check all of those invalid namespaces:
if( /^[\w\.]$/.test( namespaceStr ) ) {
//valid namespace
} else {
//invalid namespace
}
Any better suggestion for a small and efficient way to check if a string represents a valid namespace?
Here is a little jsfiddle that you can use for testing this regular expression: http://jsfiddle.net/bA85y/
Edit: This one should work for every case:
/^(?:[a-z]\d*(?:\.[a-z])?)+$/i
If you don't care about capturing groups even shorter:
/^([a-z]\d*(\.[a-z])?)+$/i
A little explanation:
^ // Start
( // Open group
[a-z]\d* // Must start by letter and may be followed by a number (greedy)
(\.[a-z])? // It may be followed by a dot only if it's followed by a letter (non-greedy)
)+ // Close group and match at least once so we get rid of empty values
$ // Ends, not allow any other characters
Demo: http://jsfiddle.net/elclanrs/5hnQV/
Try this pattern:
/^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*$/i
EDIT:
this is a reversion of #elclanrs jsfiddle
I think you are looking for this:
/^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*$/i
EDIT:
This one is a little better (with ?: and \d inspired by #HashemQolami and #elclanrs):
/^[a-z][a-z\d]*(?:\.[a-z][a-z\d]*)*$/i
And this one is shorter but does the same job:
/^[a-z](?:[a-z\d]*(?:\.[a-z])?)*$/i
And this one too, using lookahead to test that it doesn't end with a .:
/^(?!.*\.$)(?:[a-z][a-z\d]*\.?)+$/i
Please note that the selected answer doesn't work with "a.b.c" or in some cases with more than two levels.
UPDATE:
I've made a little (very basic) test:
var valid = [
"com.company.package",
"com.company",
"com.company1",
"com1.company1",
"a.b.c",
"a1.b.c3.d",
"a1.b2.c3.d4"];
var invalid = [
"1com.company.package",
"com.1company",
"com.com%any",
".com.company",
"com.company.",
"com ",
" com",
"",
".",
"com..company"];
function testRegex(regex, list)
{
var res=[];
for(var i=0; i<list.length; i++)
{
if(regex.test(list[i]))
res.push(list[i] + " ==> matched");
else
res.push(list[i] + " ==> NOT matched");
}
return res.join('<br>');
}
var regex = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*$/i;
var html = "<p>VALID</p>";
html += testRegex(regex, valid);
html += "<p>INVALID</p>";
html += testRegex(regex, invalid);
document.write("<div>" + html + "</div>");
Based on #dionyziz answer this work:
/^[a-z]+(\.[a-z]+)*[^.\s]$/
The following regular expression will do what you need. It checks for an alphabetic string and then allows multiple other alphabetic strings separated by a dot.
/^[a-z]+(\.[a-z]+)*$/
This is the stantard string structure:
{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_timestamp":"1325711938","check_hash":"892251599922cc58"}
the only part i need from this string is the numeric value after the "target_profile_id", in this case, would be "100003221104984"
I really suck at regular expressions and any help would be really appreciated !
Thanks in advance
The data appears to be in JSON format (minus HTML escaped). As such, there is no need for a regular expression.
Instead, access it directly:
var data = {"actor":"100003221104984","target_fbid":"286108458103936", ...}
alert(data.target_profile_id);
See the fiddle.
UPDATE
As noted by Jonathan, if the string indeed includes HTML entities, you will need to first parse it to create an object to assign to data in my example above.
There are additional posts on SO that answer how to do that. For example: How to decode HTML entities using jQuery?
If you have all these &quo te; stuff you could also do it by getting the right chars, without regex
var x =
"{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_timestamp":"1325711938","check_hash":"892251599922cc58"}";
var begin = x.indexOf("target_profile_id")+ "target_profile_id".length + "":"".length;
var endString = x.substring(begin, x.length);
var end = endString.indexOf(""") + begin;
alert(x.substring(begin, end));
Try:
/"actor":"(\[^&\]+)/
or
/"actor":"([^&]+)/
I have understood your problem. The whole string is actually a JSON object, where quote(") is present in the form of " ;.
First replace & quote; it with ". Then evaluate the expression and get the value of any item you want.
Below working code :)
<script type="text/javaScript">
var str1="{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_ti":{"actor":"100003221104984","target_fbid":"286108458103936","target_profile_id":"100003221104984","type_id":"17","source":"1","assoc_obj_id":"","source_app_id":"2305272732","extra_story_params":[],"content_timestamp":"1325711938","check_hash":"892251599922cc58"}}";
var ans=str1.split(""").join("\"");
var obj=eval("(" + ans+ ')');
alert(obj.target_profile_id);
</script>
And see how your actual Data looks like:
{
"actor": "100003221104984",
"target_fbid": "286108458103936",
"target_profile_id": "100003221104984",
"type_id": "17",
"source": "1",
"assoc_obj_id": "",
"source_app_id": "2305272732",
"extra_story_params": [],
"content_ti": {
"actor": "100003221104984",
"target_fbid": "286108458103936",
"target_profile_id": "100003221104984",
"type_id": "17",
"source": "1",
"assoc_obj_id": "",
"source_app_id": "2305272732",
"extra_story_params": [],
"content_timestamp": "1325711938",
"check_hash": "892251599922cc58"
}
}