Validate time format hh:mm:ss.ms with regEx JavaScript? [duplicate] - javascript

This question already has an answer here:
Why this javascript regex doesn't work?
(1 answer)
Closed 2 years ago.
I created function that should validate user input. The value should only be accepted as valid if format is matching this hh:mm:ss.s. Here is the function:
function time_format(time_val) {
let regEx = new RegExp('/^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{2}?$/');
console.log(time_val);
console.log(regEx.test(time_val));
};
console.log(time_format('00:00:00.0'));
console.log(time_format('05:35:23.7'));
console.log(time_format('25:17:07.0'));
All three values failed the test above. First and second format should pass the regex. The third should fail since the hours are not valid. If anyone knows how this can be fixed please let me know.

Try this…
function time_format(time_val) {
let regEx = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{1,2}?$/;
console.log(time_val);
console.log(regEx.test(time_val));
};
console.log(time_format('00:00:00.0'));
console.log(time_format('05:35:23.7'));
console.log(time_format('25:17:07.0'));

Related

Parse value only from JSON string [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
An API returns a string of text that looks like this (xxx used for security):
{"xxx":{"xxx":{"xxx":{"xxx":{"results":[{"latest.GigabytesIngested":12641.824682336}]}}}}}
If I do this:
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]);
I get this, which is fine:
{ 'latest.GigabytesIngested': 12641.82487968 }
My problem is I only want to grab the number. The below attempt doesn't work, maybe because there's a dot in the key name, or maybe because I'm just doing it wrong?
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0].latest.GigabytesIngested);
#derpirscher answered correctly in a comment:
console.log(JSON.parse(body).data.actor.account.nrql.results[0]['latest.GigabytesIngested']);
Yes, the period in the key is the problem. You need to use an alternate way to reference the key.
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]["latest.GigabytesIngested"]);
or
var result = JSON.parse(body).xxx.xxx.xxx.xxx.results[0];
var lgi = result["latest.GigabytesIngested"];
console.log(lgi);

JavaScript/regex: Remove characters [duplicate]

This question already has answers here:
JavaScript/regex: Remove text between parentheses
(5 answers)
Closed 1 year ago.
Can someone show me a way to convert
moveForward('block_id_~T_o|qZc=[B}}[p5qej5');
turnLeft('block_id_r21iJfS8+9W6?pnK/=sA');
moveForward('block_id_hDK1xef|j[E2X6N{M[}o');
turnRight('block_id_R9g=)oLUt|^|gPSr!XU^');
moveForward('block_id_AS124O%V`/$a4o6ZyhQ8');
to
moveForward();
turnLeft();
moveForward();
turnRight();
moveForward();
and
while (notDone()) {
moveForward('block_id_p5Zhkkmn[TSboZ83#PNG');
}
to
while (notDone()) {
moveForward();
}
using JavaScript with Regex or any other way?
Thanks in advance.
Not sure why you want to use something as a string in your code that already looks like a code, here's the solution anyways:
Regex:
(?<![\w])(moveForward|turnLeft|turnRight)\('[^']*'\)
JS example:
let str = `moveForward('block_id_~T_o|qZc=[B}}[p5qej5');
turnLeft('block_id_r21iJfS8+9W6?pnK/=sA');
moveForward('block_id_hDK1xef|j[E2X6N{M[}o');
turnRight('block_id_R9g=)oLUt|^|gPSr!XU^');
moveForward('block_id_AS124O%V\`/$a4o6ZyhQ8');
while (notDone()) {
moveForward('block_id_p5Zhkkmn[TSboZ83#PNG');
}`
let newStr = str.replace(/(?<![\w])(moveForward|turnLeft|turnRight)\('[^']*'\)/g, "$1()");
console.log(newStr);
If you want to replace part of your (actual) code, you can use visual studio code (or any code editor that lets you find and replace using regex)

How to skip the - character inside a JSON field in JS/type script [duplicate]

This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 2 years ago.
One of the JSON field in my JSON file has the field "AlphaWorkStatusChangeInfo-Comment" and I'm trying to add value to that field. But some how the type/JS script is not accepting the "-" in the field.
let data = JSON.parse(JSON.stringify(Agri.data));
data.AlphaWorkStatusChangeInfo = ["other"];
data.AlphaWorkStatusChangeInfo-Comment = "Had to quit the job";
data.autoSave = true;
What are my options to include/Add the value in the field "AlphaWorkStatusChangeInfo-Comment".
Any help is much appreciated.
The hyphen is not valid unless you encase it as a string element like so:
data["AlphaWorkStatusChangeInfo-Comment"] = "Had to quit the job";
See here for more discussion on this topic: Which characters are valid/invalid in a JSON key name?

Date.parse() is not working properly in javascript [duplicate]

This question already has answers here:
How to check if a string is a legal "dd/mm/yyyy" date?
(7 answers)
Closed 8 years ago.
I want to know whether the given string is datetime, so i used the Date.parse() method as below
var dateType=!isNaN(Date.parse("2/3/2012")) // return true;
but for the string "2000" i am getting dateType as true
var dateType=!isNaN(Date.parse("2000")) // return true;
i want to return true when the format like "mm/dd/yyyy" is used in string not for "2000", is there any method to to like this.
Thanks in advance
Try using Regular expression to validate.
for mm/dd/yyyy format use the below regex
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
and you could test using
date_regex.test("11/09/2011")
also there are many other ways to do it. explore them also

validating internet address in javascript [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how can i validate a url in javascript using regular expression
I want to validate whether user input a valid internet adress like
www.hotmail.com
http://www.abc.com.pk
Here's the js function:
function validateString(regexPattern, testString) {
var regexObj = new RegExp(regexPattern);
return regexObj.test(testString);
}
Finding the regex for URLs is just a matter of typing it in google. Here's a good one I've used before.

Categories