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);
Related
I want to remove some special character from json without parsing
the json into object.
Parsing would result into error that is why i wanted to do without json.parse().
below is my json:
{
"id":324,
"name":"first",
"body":{
"sbody": "<p>\\\The New Stroy\\\</p>"
}
}
desired output:
{
"id":324,
"name":"first",
"body":{
"sbody": "<p> The New Stroy </p>"
}
}
Looks like your input is a string and the error you are getting is when using JSON.parse.
Try this
var response = '{"sbody": "<p>\\\The New Stroy\\\</p>"}';
response = response.replace(/\\/g, "");
var obj = JSON.parse(response);
console.log(obj);
You need to run .replace on your string:
var string = '{"id":324,"name":"first","body":{"sbody":"<p>\\\The New Stroy\\\</p>"}}';
string = string.replace(/\\/g,'');
console.log(string);
//{"id":324,"name":"first","body":{"sbody":"<p>The New Stroy</p>"}}
The reason the pattern is /\\/ is because \ is used to escape characters. With a single \ we end up escaping the /. What we need to do here is escape the escape character to turn it into a literal string character: \\.
The g after the pattern means to search for the pattern "globally" in the string, so we replace all instances of it.
var obj = {
"id":324,
"name":"first",
"body":{
"sbody": "<p>\\\The New Stroy\\\</p>"
}
}
// Convert object to string
var str = JSON.stringify(obj);
// Remove \ from the string
var convertedStr= str.replace(/\\/g,'');
// Convert updated string back to object
var newObj = JSON.parse(convertedStr);
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 = #"\&\&";
I have a string with the UTF-8 character ↵. To my understanding, if you want to replace a UTF-8 character in a string, you specify the character with its hexadecimal representation, like so:
var string = "↵↵↵Middle↵↵↵";
console.log("Match? " + /\u21b5/.test("↵"));
console.log(string);
string = string.replace("/\u21b5/g", "");
console.log(string);
It is a match, but the replace is not working. What am I missing?
JSFiddle
You are using a string not a regex
string = string.replace(/\u21b5/g, "");
replace
string = string.replace("/\u21b5/", "");
with
string = string.replace(/\u21b5/g, "");
I have one string, I want to remove some repeated part from string using ajax or javascritp.
The string is -
1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236
Above string is connect using underscore (_) sign. means above string include 3 string. I want to remove -master=122....
The '-master=' is default but after equal sign(=) number will change. So how to remove '-master=n...' from above string.
var s = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
console.log(s.replace(/-master=\d+/g, ''));
Use a replace function with a greedy /-master=\d+/ regex :
PHP
$input = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
$output = preg_replace('/-master=\d+/', '', $input);
echo $output; // 1-16-15_2-34-33_3-33-23
JS
var input = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
var output = input.replace(/-master=\d+/g, '');
console.log(output); // 1-16-15_2-34-33_3-33-23
Try this:
var str = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
str = str.replace(/-master=/g,'=');
This might be pretty simple, but I am having a hard time with it. Consider the following code:
var string = 'testingT#$^%#$ESTING__--232'
string = string.match(/^\w*$/)
if (string != null)
{
string = string.join('')
string = string.toUpperCase()
}
$('#my-input').val(string)
What I want to do, is to strip all characters that aren't alphanumeric or underscore from string, and then transform that string to uppercase.
So far I did that, it works perfectly if I don't add any special characters, but when I add - or ^ to it, for example, it deletes everything from #my-input
You can do this in one step:
string = string.replace(/[^\w]/g, '').toUpperCase();
console.log(string); //=> "TESTINGTESTING__232"
var string = string.replace(/[^a-zA-Z_0-9]/g,'').toUpperCase()
Also, do you need unicode? My regex will only match a-z, and not åÉø for example.
You need use 'global' flag in regex and remove match restriction.
var str = 'testingT#$^%#$ESTING__--232';
str = str.match(/\w+/g);
if (str !== null)
{
str = str.join('');
str = str.toUpperCase();
}
$('#my-input').val(str);