Add variable to RegEx - regex

For example, I've got var for RegEx DSX-?2
I need add this var to RegEx and get this .match(/DSX-?2/gi)

You can create a RegExp object by using the new RegExp() constructor or by assigning a RegExp literal to a variable:
var pattern1:RegExp = /DSX-?2/gi;
// or
var pattern2:RegExp = new RegExp("DSX-?2", "gi");

var reg:RegExp = new RegExp("DSX-?2", "gi");

Related

new RegExp inot working in Edge and FireFox

for some reasons new RegExp is not working in Edge and FireFox. How can I get around this?
var arrOfWordsToHighlight = ["microsoft","excel"];
function escapeRegExp(arrOfWordsToHighlight) {
return arrOfWordsToHighlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
var pattern = new RegExp("(?<![\\w-])(" + arrOfWordsToHighlight.map(escapeRegExp).join("|") + ")(?![\\w-])", "gi");
$('#job').highlightWithinTextarea({
highlight: pattern
});
See: https://jsfiddle.net/seb_london/wm9yqazj

Regex in Google App Script to enclose each word inside an Array inside quotes

For example I want to enclose each word in the following array inside quotes.
{seguridad=0, funcionalidad=1, instalaciones=si, observaciones=si,
areas=Pasillos, limpieza=no, pintura=tal vez}
Into:
{"seguridad"="0", "funcionalidad"="1", "instalaciones"="si",
"observaciones"="si", "areas"="Pasillos", "limpieza"="no",
"pintura"="tal vez"}
This is my unsuccesful script so far.
function Enclose() {
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("1iXQxyL3URe1X1FgbZ76mEFAxLnxegyDzXOMF6WQ5Yqs"));
var sheet = doc.getSheetByName("json");
var sheet2 = doc.getSheetByName("tabla de frecuencias");
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var prelast = sheet.getRange("A1:A").getValues();
var last = prelast.filter(String).length;
var json = sheet2.getRange("B11").getValues();
var regExp = new RegExp("/[\w]+", "g");
/* var match = json.replace(regExp,""); */
var match = regExp.exec(match);
sheet2.getRange("C11").setValue("\"" + match + "\"");
}
You may try the following approach :
First you wrap all , and = by quotes "" by using the following regex:
/(\s*[,=]\s*)/
Then you replace the opening brackets separately using the following two regex:
/(\s*{)/gm
/(\s*})/gm
const str = `{seguridad=0, funcionalidad=1, instalaciones=si, observaciones=si, areas=Pasillos, limpieza=no, pintura=tal vez}`;
var result = str.replace(/(\s*[,=]\s*)/gm,`"$1"`);
result=result.replace(/(\s*{)/gm,`$1"`);
result=result.replace(/(\s*})/gm,`"$1`);
console.log(result);
How about this sample?
Sample script :
var json = "{seguridad=0, funcionalidad=1, instalaciones=si, observaciones=si, areas=Pasillos, limpieza=no, pintura=tal vez}";
var res = json.replace(/(\d+|[a-zA-Z]+)=(\d+|[a-zA-Z\s]+)/g, "\"$1\"=\"$2\"");
Logger.log(res)
Result :
var json = "{seguridad=0, funcionalidad=1, instalaciones=si, observaciones=si, areas=Pasillos, limpieza=no, pintura=tal vez}";
var res = json.replace(/(\d+|[a-zA-Z]+)=(\d+|[a-zA-Z\s]+)/g, "\"$1\"=\"$2\"");
console.log(res)
When this is reflected to your script, the modified script is as follows.
Modified script :
function Enclose() {
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("1iXQxyL3URe1X1FgbZ76mEFAxLnxegyDzXOMF6WQ5Yqs"));
var sheet = doc.getSheetByName("json");
var sheet2 = doc.getSheetByName("tabla de frecuencias");
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var prelast = sheet.getRange("A1:A").getValues();
var last = prelast.filter(String).length;
var json = sheet2.getRange("B11").getValue();
// var regExp = new RegExp("/[\w]+", "g");
// /* var match = json.replace(regExp,""); */
// var match = regExp.exec(match);
match = json.replace(/(\d+|[a-zA-Z]+)=(\d+|[a-zA-Z\s]+)/g, "\"$1\"=\"$2\"");
sheet2.getRange("C11").setValue("\"" + match + "\"");
}

Character Class \w not Working?

I'm doing the following regular expression in Node:
var checkPath = '^\/path\/([\w]+)\/messages$';
var path = '/path/54946fde030ba8cc5471efc9/messages';
var match = path.match(checkPath);
This doesn't seem to work.
However, when I do this:
var checkPath = '^\/path\/([0-9a-z]+)\/messages$';
var path = '/path/54946fde030ba8cc5471efc9/messages';
var match = path.match(checkPath);
It seems to work.
What is the difference?
You may want to write like
var checkPath = '^/path/(\\w+)/messages$';
var path = '/path/54946fde030ba8cc5471efc9/messages';
var match = path.match(checkPath);
Changes made
Escape \w as \\w
\w => [a-zA-Z0-9_] hence enclosing \w in another class does not add any advantage, written simply as \w

Using encodeURIComponent in XPages

In the W3Schools they give this example:
var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab";
var res = encodeURIComponent(uri);
Which does what I want however, when I run it I get an error that encodeURIComponent is not implemented.
try{
var str:String = "Contracts and Leases";
var rtn = encodeURIComponent(str);
}catch(e){
println(e)
}
added the above to a button i get the not implemented error. Is there an equivalent I just need to replace the spaces with %20s.
Use java.net.URLEncoder.encode(stringToEncode, "utf-8") so in your case do this:
try{
var str:String = "Contracts and Leases";
var rtn = java.net.URLEncoder.encode(str, "utf-8");
}catch(e){
println(e)
}

AS3 - Replace portion of string with a variable string

Trying to replace a portion of a string with a "-" if it matches a string variable in AS3.
var re:RegExp = new RegExp(imageArray[j][1],"gi");
trace(imageArray[jTemp][2].replace(re,"-"));
imageArray[jTemp][2] is a string
imageArray[j][1] is a string as well
I'm not getting the result I expect. I would like trace above to return 'permanentContainer-' Here are the traces for the above variables
permanentContainer-temporaryContainer-
temporaryContainer
var str:String = "permanentContainer-temporaryContainer-"
var pattern:RegExp = /-[(a-z)]+-/i;
var re:RegExp = new RegExp( "-^-$", "i");
trace( str.replace(pattern,"-"));
// traces
// permanentConta1iner-
Here's what ended up working for me:
var str:String=imageArray[jTemp][2];
var pattern:String=imageArray[j][1];
var regex:RegExp=new RegExp(pattern,"ig");
imageArray[jTemp][2]=str.replace(regex,"-");