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

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

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']";

Putting together regex to highlight words inside a textbox [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 3 years ago.
Hey all I am trying to have a simple way of adding multiple item names to a regex list:
var whatToLookFor = "DOZ,DOB".split(",");
var highlightThis = "";
$.each(whatToLookFor,function(i){
highlightThis += whatToLookFor[i] + "\/|";
});
highlightThis = "\/" + highlightThis.replace(/.$/,"\/gi");
console.log(highlightThis);
/DOZ\/|DOB\//gi <-highlightThis should look like this
$('.string-example').highlightWithinTextarea({
highlight: highlightThis
});
However, I am not getting the same values shown on the page using highlightThis as I do when using the /DOZ/|DOB//gi. I output the value of highlightThis and it looks just like it should but still does not work for some reason.
$('.string-example').highlightWithinTextarea({
highlight: /DOZ\/|DOB\//gi
});
What could I be missing in order for it to treat it like a regex value? It's probably something simple that I am just overlooking. :o)
The code I am using is here
final working code
var whatToLookFor = "DOZ,DOB".split(",");
var highlightThis = "";
$.each(whatToLookFor,function(i){
highlightThis += whatToLookFor[i] + "/|";
});
highlightThis = highlightThis.replace(/.$/,"");
$('.string-example').highlightWithinTextarea({
highlight: new RegExp(highlightThis, 'gi')
});
The difference is you're creating it as a string vs as a regex. Instead of adding on gi at the end, use new RegExp and pass your string in and then your flags:
new RegExp('DOZ\\/|DOB/', 'gi');
This will output what you're expecting:
/DOZ\/|DOB//gi

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

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

Javascript match and test returning null to DOI Regex expression [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I have been trying to find a way to recognize DOI on an input form to trigger a specific search.
I found a DOI regular expression here and when I use it with Match or Test I got 'NULL' and Error as result
function checkDOI(string){
//Redundant. I know
var testKey = String(string);
var DOIpattern = '\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b';
var found = DOIpattern.test(testKey);
console.log("found", found + " DOI "+ testKey);
return found
}
checkDOI("10.1016.12.31/nature.S0735-1097(98)2000/12/31/34:7-7")
I got this error DOIpattern.test is not a function
Then if I change the found.test for MATCH var found = DOIpattern.match(testKey);
The result is NULL
Does anybody can tell me what I am doing wrong?
Thank in advance!
test is a method of RegExp class and not of String. To correct, create a RegExp object using the constructor and call the test method on it.
function checkDOI(string) {
//Redundant. I know
var testKey = String(string);
var DOIpattern = '\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b';
var found = new RegExp(DOIpattern).test(testKey);
console.log("found", found + " DOI " + testKey);
return found
}
checkDOI("10.1016.12.31/nature.S0735-1097(98)2000/12/31/34:7-7")
As long as your RegExp string is correct, the input should match.
create a RegExp object using the constructor and call the test method on it.
Thank you! your response worked. Just in case someone else is looking for a regular expression the one I am using is not properly written, so I cut it to this
\b(10[.][0-9]{4,}(?:[.][0-9]+)*\b/g
And the final version looks like this:
checkDOI(string){
var DOIpattern = new RegExp(/\b(10[.][0-9]{4,}(?:[.][0-9]+)*)\b/g);
// var DOIpattern = ;
var found = DOIpattern.test(testKey);
console.log("found", found + " DOI "+ testKey);
return found
}

Javascript RegExp: How to remove all charcters after // until a new line [duplicate]

This question already has answers here:
RegEx for match/replacing JavaScript comments (both multiline and inline)
(17 answers)
Closed 5 years ago.
Basically what I want to do is parse code, and remove all comments made with "//", including the "//", until a new line appears. I don't know how to effectivly do it in Regex unfortunately.
Some example code might look like this:
variable += 10; //comment to be removed.
more code...
so only "//comment to be removed." gets removed
console.log(`something // awodkajwodkjoawjdojawdjk
another thing // fgskgkjhgkf
last thing`.replace(/\/\/.*/g, ''));
This will allow you to parse the code, instead of just removing the comments from a string:
var code = getCode();
var stripped = '';
var regex = /(.*)\/\/.*/g;
var result;
while ( result = regex.exec( code ) ) {
console.log( result );
// Do other stuff with line of code
stripped += ( result[1] + '\n' );
}

Categories