JS change especially date format from string [duplicate] - javascript

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
It is the first time when I met some of the bad example of data format.
All date goes for me regarding to a region of a customer.
I cannot change it. I got string:
var str = "23.01.2019 23:05:58";
in order to get
var dt = new Date(str);
I need to execute at least
str.replace(/./g,'/');
But got a weird result. (tried "split" and "join") also nothing.
Thanks a lot for any help

The . in your regular expression means any character.
Try this instead...
str.replace(/\./g,'/');
By using \. you're "escaping" the dot and saying "this has to be a dot character"

Related

Replace String with value `$&' not working in javascript [duplicate]

This question already has answers here:
Why 'ABC'.replace('B', '$`') gives AAC
(2 answers)
Closed 1 year ago.
I have string show {{value}} and I want replace {{value}} with $& but it not work. It return current value show {{value}}.
Here is my code
let data ="show {{value}}";
let output = data.replace("{{value}}","$&");
alert(output);
I don't know why it not work. I try replace with other strings same $1, $a and it work.
How I can fix my problem
$ is a special symbol in javascript. Write $$& instead and it should work :)

Regex how to get what comes after a certain word in a string? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 2 years ago.
http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903
I am trying to get whatever comes after 'code=' from this string. How could I do this?
You could use
code=(.+)
See a demo on regex101.com.
In JavaScript this could be:
let string = 'http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903';
let m = string.match(/code=(.+)/);
console.log(m[1]);
But it would possibly be more straight-forward to parse the url as it is and use the query part accordingly.
str.match(/code=(.*)/)[1] will do that for you.

Javascript Regex not working while used in a angular controller [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Match exact string
(3 answers)
Closed 5 years ago.
I have written the following function in javascript to validate a string that I will use as a file name. I would like to check for all the characters that are restricted by Windows OS as invalid while creating files. I checked the regular expression at RegExr, it seems to be working as expected but it doesn't work when called from an Angular controller and it only matches the first character in the parameter. I'm adding the file extension later on so that isn't a problem.
Can anybody help with it? I'm relatively new to regular expressions and would appreciate any help or pointers to useful resources.
function validateInput(value) {
if (!AngularUtils.isUndefinedOrNull(value)) {
var regex = new RegExp("[^<>/\\\\=|:*?\"]+$");
var regexOutput = regex.test(value);
if (!regex.test(value))
return true;
}
return false;
}
Edit:
Even after changing the regex to handle javascript constructors, I'm still getting valid matches for the following input: "sample_css", "sample=css","=sample"
Only the first string should be valid. jsfiddle here.

Complicated JavaScript string removal/replacement [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 7 years ago.
I have a query string that I need to remove a certain parameter from. For instance, my query string may be "?name=John&page=12&mfgid=320", and I need to remove the "page" parameter from it and end up with "?name=John&mfgid=320". I cannot assume that the "page" parameter is or isn't followed by other parameters.
All my attempts at using JavaScript functions/regex are failing miserably, so I could really use a hand in getting this working. Thanks.
That's quite easy... It's just /page=\d+&?/
var uri = '?name=John&page=12&mfgid=320';
uri = uri.replace(/page=\d+&?/,'');
You can use:
uri = uri.replace(/[?&]page=[^&\n]+$|([&?])page=[^&\n]+&/g, '$1');
RegEx Demo
We'll need to use alternation to cover all the cases of presence of query parameter. Check my demo for all test cases.

How to check if a date string already has two forward slashes in it? [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 need your help,
Is there a way to code a javascript function that would regex and test a string or using any other means to check if a date string already has the two forward slashes in it? ie. dd/mm/yyyy
regex('04/07/2014') { return true }
Thanks in advance for all your help?
You can use a regex test:
/\/.*\//.test("04/07/2014")
You can use this regex:
/([^/]*\/){2}/
to check if input has at least 2 forward slashes.
use /\/.*\//.test( str ) it returns true if str has two forward slashes in it.

Categories

Resources