new RegExp inot working in Edge and FireFox - regex

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

Related

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

req.params is working with $regex of mongodb [duplicate]

I have a find statement like this
collSession.find({"Venue.type": /.*MT.*/}).toArray(function (err, _clsSession)
{
console.log(_clsSession);
});
It is giving answer.But i need to some value of variable instead of that harcoded value MT.
How to achieve this ?
Thanks.
UPDATE I tried like "/."+searchterm+"./"
Its not working.
Instead of using the inline syntax to create a regular expression, you can also use the RegExp object to create one based on a string
var searchPhrase = "MT";
var regularExpression = new RegExp(".*" + searchPhrase + ".*");
collSession.find({"Venue.type": regularExpression}) [...]
Replace /.*MT.*/ with new RegExp( ".*" + variable + ".*" )
Try this:
var pattern = 'concatenate string' + here,
regexp = new Regexp(pattern);
Finally i got from here
it is "Venue.type": new RegExp(queryParams.et)
Take a look at this code: (I'm using mongoose)
exports.getSearchPosts = (req, res, next) => {
const keyword = req.body.keyword;
Post.find({ postTitle: new RegExp( ".*" + keyword + ".*" ) }).then(posts => {
res.render('post/search', {
pageTitle: 'Search result for: ' + keyword,
posts: posts,
category: postCategory,
posts: catPost,
});
}).catch(err => console.log(err));
}
I think you will find it helpful

Reg-ex query is too greedy

Consider the following snippet:
<Offering id=1 blah blah templateid=abc something=blah
gretre
rtert
ret
tr
/Offering>
<Offering id=2 blah blah templateid=def something=blah>
gretre
rtert
ret
tr
</Offering>
<Offering id=3 blah blah templateid=ghi something=blah>
gretre
rtert
ret
tr
</Offering>
Given that all I know is the template id, I need to return the whole Offering node that contains it. i.e. for templateid=def, I need to return:
<Offering id=2 blah blah templateid=def something=blah>
gretre
rtert
ret
tr
</Offering>
I've tried all sorts but the closest I can get is something along the lines of (?s)<Offering.+?templateid=def.+?</Offering> which returns from the first offering until the end of the offering containing my template id. I understand why but nothing I've tried can fix it. I'm guessing lookarounds but I just can't get it right.
How can I return the whole offering node?
You could modify your regex using negation and I would probably use a word boundary as well.
<Offering[^>]*\btemplateid=def[^>]*>[^<]*</Offering>
If you have other tags inside of this tag, you could do ...
(?s)<Offering[^>]*\btemplateid=def.+?</Offering>
This should work but please notice that I escaped the / character, and you may not need to do that depending on what language you're using:
(<Offering[^>]* templateid=ghi [^>]*>[^<]*<\/Offering>)
As you say you "need to return the whole Offering node", the arguably simpler, safer and more readable way would be a DOM parser. I've included examples of how you might do this in JavaScript and PHP below.
PHP
$doc = new DOMDocument();
#$doc->loadHTML($testStr); //Only needed if you're loading HTML like in the example which has repeated attributes and other things that could cause errors
$body = $doc->getElementsByTagName('body')->item(0);
$templateID = 'def';
$myNode = null;
foreach($body->childNodes as $node)
{
if($node->nodeName=='offering')
{
if($node->attributes->getNamedItem('templateid')->nodeValue == $templateID)
{
$myNode = $node;
}
}
}
//$id = $myNode->attributes->getNamedItem('id')->nodeValue;
//$html = $doc->saveHTML($myNode)
JavaScript
var testStr = document.getElementById('str_container').innerHTML;
var parser = new DOMParser();
var doc = parser.parseFromString(testStr,'text/html');
var templateID = 'def';
var myEl = null;
for(var i=0,c=doc.body.children.length;i<c;i++)
{
if(doc.body.children[i].getAttribute('templateid')===templateID)
{
myEl = doc.body.children[i];
}
}
//var id = myEl.id;
//var html = myEl.outerHTML;
console.log(myEl || 'not found');
JavaScript >= IE8
var testStr = document.getElementById('str_container').innerHTML;
var parser = new DOMParser();
var doc = parser.parseFromString(testStr,'text/html');
var templateID = 'def';
var myEl = doc.body.querySelector('offering[templateid='+templateID+']');
//var id = myEl.id;
//var html = myEl.outerHTML;
console.log(myEl || 'not found');

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