is it possible to concat 2 regular expression strings in javascript [duplicate] - javascript

This question already has answers here:
Combining regular expressions in Javascript
(6 answers)
Closed 4 years ago.
I have 2 regex strings in javascript and I need to concat them into 1 regex. I saw somewhere this could be done using |
example:
passwordRegex:RegExp = '(?=.*[A-Za-z])(?=.*\\d)(?=.*[$#$!%*#?&^])[A-Za-z\\d$#$!%*#?&^]{8,}';
hasFourConsecutiveRegex:RegExp = '(.)\\1\\1\\1';
combinedRegex:RegExp = new RegExp(this.passwordRegex.source + ' | ' + this.hasFourConsecutiveRegex.source );
is this how its done?

You can do like this:
var passwordRegex = new RegExp('(?=.*[A-Za-z])(?=.*\\d)(?=.*[$#$!%*#?&^])[A-Za-z\\d$#$!%*#?&^]{8,}');
var hasFourConsecutiveRegex = new RegExp('(.)\\1\\1\\1');
var combinedRegex = new RegExp(passwordRegex.source + ' | ' + hasFourConsecutiveRegex.source );

Related

Include single quotation in url string in Apps Script [duplicate]

This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 6 months ago.
What is the correct way to pass the single quotes inside the [ ] in this url please in my Apps Script?
https://{{domain}}/api/1.0.0/{apiKey}/appointments?where=['start,>=,2017-05-26T07:23:46-07:00','end,<=,2017-05-26T07:23:46-07:00']
I have:
var url = 'https://{{domain}}/api/1.0.0/' + {apiKey} + '/appointments?where=[' + ? + 'start,>=,2017-05-26T07:23:46-07:00' + ? +',' + ? + 'end,<=,2017-05-26T07:23:46-07:00' + ? + ']'
Why not simply use double quote ".
var url = "https://{{domain}}/api/1.0.0/" + {apiKey} + "/appointments?where=['start,>=,2017-05-26T07:23:46-07:00','end,<=,2017-05-26T07:23:46-07:00']";

How to add empty space at the end of string? [duplicate]

This question already has answers here:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 8 months ago.
Currently, I got let str = 'prefix' which has length of 6, but I would like to add empty space at the end and update it to str = 'prefix ' with length of 7.
I have tried str.split("").push("").join("") but I get error saying join is not a function.
I have also tried str[str.length] = ' ', but this doesn't work either. What other options do I have?
You can concatenate the strings:
let str = 'prefix';
str = str + ' ';
// str === 'prefix '
or, if you want to pad any sized string to a specified length:
let str = 'prefix';
str = str.padEnd(7, " ");
// str === 'prefix '

insert variable via forEach into regex expression [duplicate]

This question already has answers here:
Use dynamic (variable) string as regex pattern in JavaScript
(8 answers)
Closed 4 years ago.
As you can see below, I'm trying to count how many times a character in string J occurs in string S. The only issue is I can't put the argument o in the forEach loop into the regex expression as shown in the console.log.
var numJewelsInStones = function(J, S) {
let jArr = J.split('');
let sArr = S.split('');
jArr.forEach(o=>{
console.log(S.replace(/[^o]/g,"").length);
})
};
numJewelsInStones("aA", "aAAbbbb");
You can create regular expression with constructor function where you pass string parameters:
new RegExp('[^' + o + ']', 'g')
Your replace logic might look like:
S.replace(new RegExp('[^' + o + ']', 'g'), '')

How to dynamically create regex to use in .match Javascript? [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 8 years ago.
I need to dynamically create a regex to use in match function javascript.
How would that be possible?
var p = "*|";
var s = "|*";
"*|1387461375|* hello *|sfa|* *|3135145|* test".match(/"p"(\d{3,})"s"/g)
this would be the right regex: /\*\|(\d{3,})\|\*/g
even if I add backslashes to p and s it doesn't work. Is it possible?
RegExp is your friend:
var p = "\\*\\|", s = "\\|\\*"
var reg = new RegExp(p + '(\\d{3,})' + s, 'g')
"*|1387461375|* hello *|sfa|* *|3135145|* test".match(reg)
The key to making the dynamic regex global is to transform it into a RegExp object, and pass 'g' in as the second argument.
Working example.
You can construct a RegExp object using your variables first. Also remember to escape * and | while forming RegExp object:
var p = "*|";
var s = "|*";
var re = new RegExp(p.replace(/([*|])/g, '\\$1')
+ "(\\d{3,})" +
s.replace(/([*|])/g, '\\$1'), "g");
var m = "*|1387461375|* hello *|sfa|* *|3135145|* test".match(re);
console.log(m);
//=> ["*|1387461375|*", "*|3135145|*"]

Why my string replace is not working in JavaScript? [duplicate]

This question already has answers here:
JS replace not working on string [duplicate]
(2 answers)
Closed 8 years ago.
I'm trying to replace a placeholder in a string I'm generating.
My string looks like this:
var s = 'module("SlapOS UI Basic Interaction"); ' +
'asyncTest( "${base_url}", function() { ' +
' expect( __number__ ); ' +
' ok(testForElement("div#global-panel"), "element present");' +
' start(); })';
And I want to replace __number__.
I can get the index correctly like so:
s.indexOf("__number__");
but replacing does not work...
s.replace("__number__", "1");
Question:
What am I doing wrong here? Makes no sense to my why it does not work.
The replace method does not modify the existing string. It returns a new one.
var result = s.replace("__number__", "1");

Categories