JavaScript/regex: Remove characters [duplicate] - javascript

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)

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);

Convert camel case string into all caps separating underscores in javascript [duplicate]

This question already has answers here:
Javascript convert PascalCase to underscore_case/snake_case
(11 answers)
Closed 2 years ago.
So I have a list of variables in the file which I want to convert into all capitals separating underscores in javascript.
variable pattern is like this:
AwsKey
AwsSecret
CognitoUserPool
which I want to convert like below:
AWS_KEY
AWS_SECRET
COGNITO_USER_POOL
how do I write a function which does this in javascript?
Any help would be much appreciated.
Thank you.
Edit: Sorry I forgotten to make them upper case
function camelToCaps(str) { return str.replaceAll(/([A-Z])/g, '_$1').replace(/([a-z])/, '$1).toUpperCase().slice(1); }
const camels = [
'AwsKey',
'AwsSecret',
'CognitoUserPool',
];
function camelToCaps(str) { return str.replaceAll(/([A-Z])/g, '_$1').toUpperCase().slice(1); }
const caps = camels.map(camelToCaps);
console.log(caps);

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

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'));

Simple format of text on front-end of my website (replace characters)? [duplicate]

This question already has answers here:
Remove first character from a string if it is a comma
(4 answers)
Closed 3 years ago.
My website uses product references in this style: "R202020"
I want them to be shown like this for the users of my website: "BA2202020"
So basically I'm looking for a script, which formats the style of my reference numbers (should affect a ".reference" class I've created) by:
Removing the "R" in the original reference - replacing it with a "BA2" in stead - leaving the rest as it is (the "202020" part).
How can I do this?
Find 1st character of your string using string[0] and replace that with your desire value like below.
var string=$('.YourClass').text();
var result = string.replace(string[0],'BA2');
$('.YourClass').text(result);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class='YourClass'>R202020</span>
Try replace method. https://www.w3schools.com/jsref/jsref_replace.asp
'R202020'.replace('R2','BA2') // BA202020

Need a Javascript URL Regex [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
Wondering if someone could help with out with creating a regex..
Basically, taking an iFrame's src, and seeing if it's from SoundCloud. If it is, return its id. For example:
var src = 'http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F85110490&auto_play=false&show_playcount=false&show_user=false&show_comments=false&buying=false&liking=false&sharing=false&show_artwork=false&color=00e7ff';
function soundcloudId(src) {
var p = /___________/;
return (src.match(p)) ? RegExp.$1 : false;
}
soundcloudId(src);
And as a result, it would run the "src" through the regex, and if a soundcloud link, would return 85110490. Otherwise, false.
Try this regex:
/http:\/\/w.soundcloud\.com\/.*%2Ftracks%2F([0-9A-F]+)/
Runnable example: http://jsfiddle.net/mYf6P/

Categories