How to replace [[ in javascript [duplicate] - javascript

This question already has answers here:
How to remove square brackets in string using regex?
(6 answers)
Replace Square Brackets for links on web page
(2 answers)
Closed 3 years ago.
I want to replace [[ with [ using javascript but my code does not work. Please help me. Thank you
I tried this code before:
this.pairs = this.pairs.replace(/[[/g, '[')
this.pairs = this.pairs.replace('[[', '[')

square brackets [ have a special meaning in RegExp, and that is for Character Classes. You need to escape the brackets in order for it to be considered a literal string:
let result = '[[ This contains [ some brackets[['.replace(/\[\[/g, '[')
console.log(result)

Related

How to backslash in number using javascript [duplicate]

This question already has answers here:
How do I put a single backslash into an ES6 template literal's output?
(2 answers)
Closed 4 months ago.
How to add backslash in a number dynamically in JavaScript.
I want output like this : '/(123) 456/-7890'
let number = '1234567890';
let test = `\(${number.substr(0,3)}) ${number.substr(3,3)}'\'-${number.substr(6,4)}`;
Backslash removed after getting the output '(123) 456-7890'
You need to double the \ because it is a special escape character.
let number = '1234567890';
let test = `\\(${number.substr(0,3)}) ${number.substr(3,3)}'\\'-${number.substr(6,4)}`;

How to retrieve the character between the "_" in the following string - ID_123_george using JAVA SCRIPT? [duplicate]

This question already has answers here:
Regex string between underscore
(2 answers)
Closed 2 years ago.
In the above string, I want to retrieve 123 only which is between the _ (underscore) character. Can someone please help me using JAVA SCRIPT?
You should split your string by _ underscore delimiter. You will have an array which contains the parts of the "original" string. You can get the parts of the "original" string by using the indexes. You can see a snippet below.
Code part:
var str = 'ID_123_george'.split('_'); //SPLIT INTO ARRAY
str[1]; //1st index will give you 123

How to give input to replace() when the string is present in an array [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 3 years ago.
I have to replace the special characters in my string with underscore. replace() function is used to do it . I know that.
My string is "545123_Claims#Claims#Claims000117".
But the issue is that replace() accepts sting as input.
Actually my string is in an array like filArr= ["545123_Claims#Claims#Claims000117"].
So how can I replace the special character in thisstring which is inside an array?
You could map the replaced strings by taking a function.
const replacementFn = string => string.replace(/xxx/, 'yyy');
filArr = fillArr.map(replacementFn);

Replace values between parentheses using Javascript and Regex [duplicate]

This question already has answers here:
How can I replace a string in parentheses using a regex?
(4 answers)
Closed 7 years ago.
I need to replace the text between two parentheses using Regex in Javascript. For example:
var x = "I need to go (now)";
I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:
x.replace(/\(now)\b/g, 'tomorrow');
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');
You don't need the \b and you need to escape the second ).

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

Categories