Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following datetime string: 09/07/2020 02:00
I need to check the format of the string before apply Date.parse() to ensure that it's in the correct format. I've developed a regular expression for the string however it doesn't appear to work in Javascript. I've tested it and it works in regex tester but not in js. Example (regex101) here
REGEX
String
09/07/2020 02:00
Expression
^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$
function clean_inputdatetime(datetime) {
var cleaned_datetime = null
var datetime_regex_cond = RegExp('/^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$/', 'g')
var regex_success = datetime_regex_cond.test(datetime)
if (regex_success) {
var cleaned_datetime = Date.parse(datetime)
}
return [regex_success, cleaned_datetime]
}
console.log(
clean_inputdatetime("09/07/2020 02:00")
)
Note regex_success returns false for above.
When using RegExp you need to remove the enclosing slashes - otherwise they will be included in the regex itself. Also you need to correctly escape the string:
const datetime_regex_cond = new RegExp('^([1-9]|([012][0-9])|(3[01]))(\\/)([0]{0,1}[1-9]|1[012])(\\/)\\d\\d\\d\\d (20|21|22|23|[0-1]?\\d):[0-5]?\\d$','g')
Or simply do:
const datetime_regex_cond = /^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$/g;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I have the following string format : 2022-10-11 13:54:41.96 and want to convert it to epoch time using JavaScript , i tried the following but get a 'NAN' ,any idea how to convert such string to epoch using javascript?
var t ='2022-10-11 13:54:41.96'
var x = new Date(t).getTime()
You can simply replace " " with the separator "T" and use new Date() with the value.
var t ='2022-10-11 13:54:41.96'
var x = new Date(t.replace(" ", "T")).getTime()
console.log(x);
If you can control the time in "t" then simple add the separator "T":
var t ='2022-10-11T13:54:41.96'
var x = new Date(t).getTime()
console.log(x);
Try this should do the trick
var t ='2022-10-11 13:54:41.96'
var x = Number(new Date(t))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I wanted to interpolate variables in strings in JS so I used ``(backticks) as shown here -
How To Interpolate Variables In String in JS
Then, I Wanted To put IF-Statements in jQuery Append So I got this -
IF Statements In jQuery Append
But When I use Both Together , Backticks Don't Output Text As Usual-
$("main").append(`Hello ${my_var}`+(second_var>1?"hi ":"bye")+`Bye ${my_var})`
This Results Only In "hi" , The Backticks Before And After The Ternary Operator Don't Output Anything.
HELP ??
You can do something like the below.
const my_var = "Name";
const seconde_var = 2;
console.log(`Hello ${my_var} ${seconde_var >1 ? "hi": "bye"} bye ${my_var}`);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So I have a function that will be called with a 1 word string.
Test("ABCD")
my code:
function Test(x)
var str=""
str.push($)
x.toLowerCase()
return str
//So I want the output to change the "ABCD" into "abcd$"
You are accepting the argument x in the function and running toLowerCase on it. The output is not having any value from the input.
Also there is no push method defined for string. It can be String.concat. Even that is not needed, you could make use of arithematic + itsel for concatenation or string leterals.
Just lowercase the input append a $ symbol. Its done!!
Im making use of .toString() method aswell. To ensure code is not breaking for other input types
It should be
function Test(x) {
return x.toString().toLowerCase() + '$'
}
console.log(Test("ABCD"));
Or Simply.
Test = (x) => `${ x.toString().toLowerCase() }$`;
console.log(Test("ABCD"));
This is just an example, please change the text which you want to add at end as you wish.
I passed the string in function through console.log but you can call the function as you wish without console.log.
function capFirst(str) {
return str.toLowerCase().concat('', '$');
}
console.log(capFirst('ABCD'));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So it says this question below is a bad question because it has bad syntax...which I wouldn't have figured out if I didn't ask it, so not sure why this counts against me for possibly not enabling me to ask questions. So I click delete and it won't let me delete the question either. So what am I supposed to do?
I have a regular expression which allows special characters with the exception of < and > in the input and from html and it works like so:
<input type="text" name="logoName[]" class="modal-text" placeholder="Name" pattern="^[a-zA-Z0-9!##$%^&*()_+/\-=,.;:'/\?/\//\\ ]+">
In other places, I need to write this pattern from javascript and visual studio code doesn't like it one bit:
const regexPattern = '^[a-zA-Z0-9!##$%^&*()_+/\\-=,.;:'/\?/\//\\ ]+';
I also tried:
function escapeRegex(string) {
string = string.toString();
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
const regexPattern = escapeRegex(/^[a-zA-Z0-9!##$%^&*()_+/\-=,.;:'/\?/\//\\ ]+/g);
and no luck with that method either.
Can someone please help? When it comes to regular expressions I'm not very good, I've tried all sorts of ways of escaping but I'm not grasping it. thanks.
I just found a free formatter online tool and this below works for me, I think the single quotes were messing me up too:
const regexPattern = "^[a-zA-Z0-9!##$%^&*()_+\/\\-=,.;:'\/\\?\/\\\/\/\\\\ ]+";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In JavaScript code I use regular expression match() function to find pattern and show the result, but it does not give a proper answer, means finding [i] character in giving sentences
<button onclick = "searchPattern()">pattern</button><br>
<p id = "demo"></p>
<script>
function searchPattern(){
var str = "Visti W3Schools!";
var paat = /[i]/g;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>
Read your JavaScript console
Uncaught ReferenceError: patt is not defined
You changed your variable name from paat to patt half way through your code.