How to Escape hyphen-minus sign in MongoDB Text Search - javascript

I need to search -2 in my database. But hyphen-minus is a special character for neglation. How can I escape hyphen-minus to find sentences that contains -2 in it.
My query:
Message.find({ "$text": { $search: "-2", $caseSensitive: false } })
Best

Try the below, You have to escape '-' by \
Message.find({ "$text": { $search: "*\-2*", $caseSensitive: false } })

I solved it with exact matching like "-2".

Related

chrome declarativeNetRequest append matched url not working

I'm trying to append the matched domain from a declarativeNetRequest rule to the redirect extension page, but I can't seem to be able to get it to work. The redirect is working to my extension page but the matched URL isn't appended.
Here is my code snippet:
const page = chrome.runtime.getURL('/MyPage.html');
const RULES = [
{
'id': 1,
'priority': 2,
action: {type: 'redirect', redirect: {regexSubstitution: page + '#\\0', extensionPath: '/MyPage.html'}},
'condition': {
regexFilter: "\w*",
requestDomains: ["amazon.com"]
}
}]
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: RULES.map(r => r.id),
addRules: RULES,
});
Updated code snippet:
const page = chrome.runtime.getURL('/MyPage.html');
const RULES = [
{
id: 1,
priority: 2,
action: {type: 'redirect', redirect: {regexSubstitution: page + '#\\1' }},
condition: {
regexFilter: "https://([^/]+)",
requestDomains: ["amazon.com"]
}
},
];
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: RULES.map(r => r.id),
addRules: RULES,
});
Remove , extensionPath: '/MyPage.html' as you already have a substitution
Replace \w* with ://([^/]+) to capture dots and dashes in the domain name, also note that inside regexp strings you need to use an escaped backslash \\ not \ while there's no need to escape the forward slash /.
replace #\\0 with #\\1 to get the parenthesized group of the above regexp.

Need to parse JSON string with value is quoted curly braces

I need to parse JSON string.
I've tried JSON.stringify and then JSON.parse below sample string, but server performed escape sequencing
I used str.replace('/\\/g','') to remove the escape sequence but that doesnt help because if you look in the "default_request" key is wraps its value with "" which is doesnt allow me parse it using JSON.parse()
{
"request": {
"service_name": "authService",
"url": "https://some-url.com/{accounts}",
"default_request": "{\"authMethod\":\"somename\",\"multiCheck\":false}"
}
}
so I tried to replace "{ with { and }" with }
str.replace('/"{/g','{')).replace('/}"/g','}'))
but it creates another problem.
Favourable condition
{
"request": {
"service_name": "authService",
"url": "https://some-url.com/{accounts}",
"default_request": {\"authMethod\":\"somename\",\"multiCheck\":false}
}
}
default_request was stringifyied twice. to fix it, try this
jsonObject.request.default_request = JSON.parse(jsonObject.request.default_request);

Regex in validate.js not working as expected

I have the below rules and using validate.js but regex does not seem to work. Below is my code.
Condition given : Check if string has at least one capital letter, at least one symbol and at least one number
password: {
presence: {
message: '^Please enter a password'
},
length: {
minimum: 5,
message: '^Your password must be at least 6 characters'
},
format: {
pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\w~##$%^&+=`|{}:;!.?\""()\[\]-]{1,}$/,
message:
'^Must contain a capital, lowercase, number and a special character!'
}
}

Codemirror Simple Mode - Regex lookbehind and lookahead does not work

I just implemented a simple mode to recognize latex code.
Here is an code example that could be highlighted:
\documentclass{test}
There should be everything blue, except of the 'test' what should be purple. This is how my implementation looks like atm:
const CodeMirror = require('codemirror')
CodeMirror.defineSimpleMode("simplemode", {
start: [
{
regex: /(?<=\{).+?(?=\})/,
token: 'argument'
},
{
regex: /%.*/,
token: 'comment'
},
/*{
regex: /\\.*{.*}/,
token: 'tag'
},*/
{
regex: /\$.*\$/,
token: 'math'
}
],
meta: {
dontIndentStates: [],
lineComment: '%'
}
})
I removed the tag part, because I thought it would overlap with the tag. Anyways, even if all regexes match perfect in regex testers for javascript, the lookahead and lookbehind do not work.
Is there any workaround, fix, mistake?

Javascript regExp second match

I have a string and want to find the second match:
string
function (typt,tyu,tyui) {
return artigos.crudButtons(true, true, true);
}
regExpx
\(([^)]+)\)
The result is
[
"(typt,tyu,tyui)",
"typt,tyu,tyui"
]
but i need
[
"(true, true, true)",
"true, true, true"
]
I need to ignore first ocurrence or find after crudButtons.
Thanks in advance
You can find the data after crudButtons as
regex.exec(text.slice(text.indexOf('crudButtons')))

Categories

Resources