Character Class \w not Working? - regex

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

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

findText find something containing a quote

I have a script that takes the current document and looks for a user defined string using findText. If that string contains a quote (like: Bob's Burgers). findText does not find it. I know it uses regular expressions, but I cannot figure out how to format the expression so it finds this properly.
code example:
var target = "Bob's Burgers";
var body = DocumentApp.getActiveDocument().getBody();
try
{
var searchResult = body.findText(target);
//does not find the text. But can find Bob easily.
}
catch(e) { ...}
I tried the findText method using the code from this post:
function highlightText(findMe) {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText(findMe);
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Where in the Element is the found text?
var start = foundElement.getStartOffset();
var end = foundElement.getEndOffsetInclusive();
// Change the background color to yellow
foundText.setBackgroundColor(start, end, "#FCFC00");
// Find the next match
foundElement = body.findText(findMe, foundElement);
}
}
function myFunction() {
highlightText("Bob’s Burger");
}
Result:
Hope this helps.
Use \` instead of ` to escape it.
var target = "Bob\'s Burgers";

How to set a RegExp to have a real matching judgement?

I would like to set a RegExp to have a matching judgement with String. I tried to use pattern.test(str), but if pattern is a part of string it will be true. It isn't what i want.
For example, I set my pattern is "/[a-z]/" and str is "abc123", it will be true by pattern.test(str). My prefer result is false, because the str contains "123", not every character is a-z. How can I do that? Thank you!
package {
import flash.display.Sprite;
public class regexp extends Sprite{
var pattern:RegExp=/[a-z]/;
var str:String="abc123";
public function regexp() {
trace(pattern.test(str));
}
}
}
Try this one:
var pattern:RegExp=/^[a-z]+?$/;

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

Add variable to 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");