Let us say we have following regex code lines in Javascript:
.replace(/[\*\+\-=~><\"\?^\${}\(\)\:\!\/[\]\\\s]/g, '\\$&') // replace single character special characters
.replace(/\|\|/g, '\\||') // replace ||
.replace(/\&\&/g, '\\&&') // replace &&
.replace(/AND/g, '\\A\\N\\D') // replace AND
.replace(/OR/g, '\\O\\R') // replace OR
.replace(/NOT/g, '\\N\\O\\T'); // replace NOT
I am trying to translate these regex code lines to following C# Regex Expressions:
public static String ReturnSanitizedString(string query)
{
String pattern1 = #"[\*\+\-=~><\""\?^\${ }\(\)\:\!\/[\]\\\s]"; // Replace the single character special characters.
String pattern2 = #"\|\|";
String pattern3 = #"\&\&";
String pattern4 = #"AND";
String pattern5 = #"OR";
String pattern6 = #"NOT";
String replacement1 = "\\$&";
String replacement2 = "\\||";
String replacement3 = "\\&&";
String replacement4 = "\\A\\N\\D";
String replacement5 = "\\O\\R";
String replacement6 = "\\N\\O\\T";
Regex rgx = new Regex(pattern1);
string result1 = rgx.Replace(query, replacement1);
Regex rgx2 = new Regex(pattern2);
string result2 = rgx2.Replace(result1, replacement2);
Regex rgx3 = new Regex(pattern3);
string result3 = rgx3.Replace(result2, replacement3);
Regex rgx4 = new Regex(pattern4);
string result4 = rgx4.Replace(result3, replacement4);
Regex rgx5 = new Regex(pattern5);
string result5 = rgx5.Replace(result4, replacement5);
Regex rgx6 = new Regex(pattern6);
string finalResult = rgx6.Replace(result5, replacement6);
return finalResult;
}
The following sentence(this is the query):
"AND there! are? (lots of) char*cters 2 ^escape!"
Should be converted to this sentence after executing above code:
\A\N\D\ there\!\ are\?\ \(lots\ of\)\ char\*cters\ 2\ \^escape\!
I am not able to get this working, what am I doing incorrect in method above.
In pattern3, you have regex \&\&| which matches basically everything.
Just remove the last pipe like this and get what you want:
String pattern3 = #"\&\&";
Related
How do I remove the characters in a string that are included in another string?
let mustRemoved = "rin8cu73s9b";
let string = "CatsAndDogsAreAwesome"
Output Must Be : atAdDogAeAweome
What I have tried:
let regEx = new RegExp(mustRemoved, "ig");
let replaceMask = "";
let final = string.replace(regEx, replaceMask);
You can convert the string into an array of characters, filter out the characters which are included in mustRemoved, then join the resulting array.
let mustRemoved = "rin8cu73s9b".toLowerCase();
let string = "CatsAndDogsAreAwesome"
let res = [...string].filter(e => !mustRemoved.includes(e.toLowerCase())).join('')
console.log(res)
You need to change your regex so that it matches with every character in mustRemoved string, like this:
let mustRemoved = "rin8cu73s9b";
let string = "CatsAndDogsAreAwesome"
let regEx = new RegExp(mustRemoved.split('').join('|'), "ig");
let replaceMask = "";
let final = string.replace(regEx, replaceMask);
console.log(final)
So I have a RegExp regex = /asd/
I am storing it as a as a key in my key-val store system.
So I say str = String(regex) which returns "/asd/".
Now I need to convert that string back to a RegExp.
So I try: RegExp(str) and I see /\/asd\//
this is not what I want. It is not the same as /asd/
Should I just remove the first and last characters from the string before converting it to regex? That would get me the desired result in this situation, but wouldn't necessarily work if the RegExp had modifiers like /i or /g
Is there a better way to do this?
If you don't need to store the modifiers, you can use Regexp#source to get the string value, and then convert back using the RegExp constructor.
var regex = /abc/g;
var str = regex.source; // "abc"
var restoreRegex = new RegExp(str, "g");
If you do need to store the modifiers, use a regex to parse the regex:
var regex = /abc/g;
var str = regex.toString(); // "/abc/g"
var parts = /\/(.*)\/(.*)/.exec(str);
var restoredRegex = new RegExp(parts[1], parts[2]);
This will work even if the pattern has a / in it, because .* is greedy, and will advance to the last / in the string.
If performance is a concern, use normal string manipulation using String#lastIndexOf:
var regex = /abc/g;
var str = regex.toString(); // "/abc/g"
var lastSlash = str.lastIndexOf("/");
var restoredRegex = new RegExp(str.slice(1, lastSlash), str.slice(lastSlash + 1));
const regex = /asd/gi;
converting RegExp to String
const obj = {flags: regex.flags, source: regex.source};
const string = JSON.stringify(obj);
then back to RegExp
const obj2 = JSON.parse(string);
const regex2 = new RegExp(obj2.source, obj2.flags);
Requires ES6+.
You can use the following before storage of your regex literal:
(new RegExp(regex)).source
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source
Example:
regex = /asd/
string = (new RegExp(regex)).source
// string is now "asd"
regex = RegExp(string)
// regex has the original value /asd/
let rx = RegExp.apply(RegExp, str.match(/\/(.*)\/(.*)/).slice(1));
A modified version of #PegasusEpsilon answer
StackOverflow saves the day again, thanks #4castle! I wanted to store some regex rules in a JS file, and some in a DB, combine them into an array of objects like so:
module.exports = {
[SETTINGS.PRODUCTION_ENV]: [
{
"key": /<meta name="generator"[\s\S]*?>/gmi,
"value": "",
"regex": true
},
...
]
}
Then, loop through each environment's objects and apply it to a string of text. This is for a node/lambda project, so I wanted to use ES6. I used #4castle's code, with some destructuring, and I ended up with this:
let content = body;
const regexString = replacement.key.toString();
const regexParts = /\/(.*)\/(.*)/.exec(regexString);
const {1: source, 2: flags} = regexParts;
const regex = new RegExp(source, flags);
content = content.replace(regex, replacement.value);
return content;
Works a treat!
I have a string which has some data with a few special characters, Need to remove the data between the desired special char in JavaScript.
The special char would be obtained in a variable.
var desiredChar = "~0~";
And Imagine this to be the Input string:
~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~
So I'm supposed to remove the text in bold.
The desired output is supposed to be-
~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~
I've tried using "Replace" and "Regex", but as the desired character is being passed in a variable and keeps changing I'm facing difficulties.
You can create your own regex based on whatever the bounding character(s) are that contain the text you want removed, and then replace any text that matches that regex with a blank string "".
The JS below should work for your use case (and it should work for multiple occurrences as well):
var originalText = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
var desiredChar = "~0~";
var customRegex = new RegExp(desiredChar + ".*?" + desiredChar, "gi");
var processedText = originalText.replace(customRegex, "");
console.log(processedText);
You can build your regex from the constructor with a string input.
var desiredChar = "~0~";
// use the g flag in your regex if you want to remove all substrings between desiredChar
var myRegex = new Regex(desiredChar + ".*" + desiredChar, 'ig');
var testString = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
testString = testString.replace(myRegex, "");
Given input string you can use .indexOf(), .lastIndexOf() and .slice().
Note, OR character | passed to RegExp constructor should be escaped to avoid RegExp created by passing string interpreting | character as OR | within resulting RegExp passed to .replace().
var desiredChar = "~0~";
var str = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
var not = str.slice(str.indexOf(desiredChar), str.lastIndexOf(desiredChar) + desiredChar.length);
// escape OR `|`
var res = str.replace(new RegExp(not.replace(/[|]/g, "\\|")), "");
console.log(res)
You can use the RegExp object:
var regexstring = "whatever";
var regexp = new RegExp(regexstring, "gi");
var str = "whateverTest";
var str2 = str.replace(regexp, "other");
document.write(str2);
Then you can construct regexstring in any way you want.
You can read more about it at http://www.regular-expressions.info/javascript.html
I am trying to eliminate some characters if found in my string like v_[i] will be v.To do this I am using RegExp but it is not capturing the group.
var string='v_[i]';
var regexExp = new RegExp('_[\w+]','ig');
var finalstring = string.replace(regexExp,'');
alert(finalstring);
While in the other hand if i use a literal string it works perfectly
var finalstring = string.replace(/_\[\w+\]/ig,'');
then why RegExp not capturing the group as the literal string does.
You should escape the brackets in your regex:
var string = 'v_[i]';
var regexExp = /_\[\w+\]/ig;
var finalstring = string.replace(regexExp, '');
How can I transform the string "Test(5)" into "Test\(5\)" dynamically? (JQuery or Javascript)
I have tried this but with no success
var string = "Test(5)";
string = string.replace("(","\(");
string = string.replace(")","\)");
console.log(string);
http://jsfiddle.net/mvehkkfe/
I assume you meant
replace the string "Test(5)" into "Test\(5\)"
In which case:
var string = "Test(5)";
string = string.replace("(","\\(");
string = string.replace(")","\\)");
console.log(string);
Escape the backslash
If you want to replace the string "Test(5)" into "Test\(5\)", you can use below code
var string = "Test(5)";
string = string.replace("(","\\(");
string = string.replace(")","\\)");
console.log(string);