Javascript search and replace sequence of characters that contain square brackets - javascript

I'm trying to search for '[EN]' in the string 'Nationality [EN] [ESP]', I want to remove this from the string so I'm using a replace method, code examaple below
var str = 'Nationality [EN] [ESP]';
var find = "[EN]";
var regex = new RegExp(find, "g");
alert(str.replace(regex, ''));
Since [EN] is identified as a character set this will output the string 'Nationality [] [ESP]' but I want to remove the square brackets aswell. I thought that I could escape them using \ but it didn't work
Any advice would be much appreciated

Try setting your regex this way:
var regex = /\[EN\]/g;

If you just want to replace a single instance of it you can just str = str.replace("[EN] ", ""); otherwise; var find = "\\[EN\\]";.

Related

How to get string between two dot (last dot followed by image extension)?

How to get string between two dot (last dot followed by image extension) ?
I try to filter the string will be delete then replace by '',
but my regex is wrong, how to solve it?
e.g
// a09 could be any letter
var str = 'https://s.domain.com/q1_1-0.a09.jpg';
I want to remove .a09 become
'https://s.domain.com/q1_1-0.jpg';
try to get .a09 then replace by ''
var str = 'https://s.domain.com/q1_1-0.a09.jpg';
var regexPattern = /(\.(.*?)\.(jpg|jpeg|tiff|png)$)/;
var removeString = regexPattern.exec(str);
console.log(removeString)
I would use a negated character class to make sure its the last dot before the extensions. Here is a solution specifically for jpg
Replace
\.[^.]*(\.jpg)
With
$1
In Javascript dialect:
result = subject.replace(/\.[^.]*(\.jpg)/g, "$1");

Regular Expression to replace part of a string

I need to replace part of a string, it's dynamically generated so I'm never going to know what the string is.
Here's an example "session12_2" I need to replace the 2 at the end with a variable. The "session" text will always be the same but the number will change.
I've tried a standard replace but that didn't work (I didn't think it would).
Here's what I tried:
col1 = col1.replace('_'+oldnumber+'"', '_'+rowparts[2]+'"');
Edit: I'm looking for a reg ex that will replace '_'+oldnumber when it's found as part of a string.
If you will always have the "_" (underscore) as a divider you can do this:
str = str.split("_")[0]+"_"+rowparts[x];
This way you split the string using the underscore and then complete it with what you like, no regex needed.
var re = /session(\d+)_(\d+)/;
var str = 'session12_2';
var subst = 'session$1_'+rowparts[2];
var result = str.replace(re, subst);
Test: https://regex101.com/r/sH8gK8/1

remove comma seperated string and replace it in javascript regex

I have a string as str = "aa#gmail.com,pp#gmailcom,cc#gmail.com,"
I am replacing the string with quotes like below
var re = new RegExp("cc#gmail.com", "g");
$('reminder_email').value= str.replace(re," ");
I am able to replace the email id that matches in regex.. But the comma still remains there. So the result will be like
"aa#gmail.com,pp#gmailcom,,"
But I need to replace the email id with comma (which comes after each one).. How to do that..
try this
demo
str = "aa#gmail.com,pp#gmailcom,cc#gmail.com,"
var re = new RegExp("cc#gmail.com,", "g");
a= str.replace(re," ");
The problem is you don't know if you'll leave a dangling leading, dangling trailing or doubled comma depending on whether the removed target is first, last or in the middle respectively.
The easy way is to clean up misplaced commas after doing the first replace:
str.replace(re," ").replace(/,,+/, ",").replace(/^,|,$/, "");

Extracting elements from a variable

I have a string 'http://this.is.my.url:007/directory1/directory2/index.html' and I need to extract the string as below. Please advice the best way
var one = http://this.is.my.url:007/directory1/directory2/index
Try this:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.[^.]*$/g, ''); // would replace all file extensions at the end.
// or in case you only want to remove .html, do this:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.html$/g, '');
The $ character when included in a regular expression matches to the end of the text string. In variant a you look start at the "." and remove everything from the this character until the end of the string. In variant 2, you reduce this to the exact string ".html". This is more about regular expressions than about javascript. To learn more about it, here is one of many nice tutorials.
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var trimmedUrl = url.replace('.html', '');
You just need to use replace():
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var one = url.replace('.html', '');
If want to ensure you only remove the .html from the end of the string use regex:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var one = url.replace(/\.html$/', '');
The $ indicates that only the last characters of the string should be checked.
Using a regular expression, it replaces everything (.*) with itself from the capture group (not including the trailing .html).
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var one = url.replace(/(.*)\.html/, '$1');
^ ^ ^^
// Capture group ______| |__________||
// Capture ----> Get captured content
You could slice the string up to the last dot:
var url = 'http://this.is.my.url:7/directory1/directory2/index.html';
url = url.slice(0,url.lastIndexOf('.'));
//=> "http://this.is.my.url:7/directory1/directory2/index"
Or in one line:
var url = ''.slice.call(
url='http://this.is.my.url:7/directory1/directory2/index.html',
0,url.lastIndexOf('.') );

Javascript regex grouping

I am trying to create a regular expression that would easily replace an input name such as "holes[0][shots][0][unit]" to "holes[0][shots]1[unit]". I'm basically cloning a HTML input and would like to make sure its position is incremented.
I got my regex built and working correctly using this (awesome) tool : http://gskinner.com/RegExr/
Here is my current regex :
(.*\[shots\]\[)([0-9]+)(\].*\])
and I am using a replace such as :
$12$3
this transforms "holes[0][shots][0][unit]" into "holes[0][shots][2][unit]". This is exactly want I want. However, when I try this in javascript (http://jsfiddle.net/PH2Rh/) :
var str = "holes[0][shots][0][units]";
var reg =new RegExp("(.*\[shots\]\[)([0-9]+)(\].*\])", "g");
console.log(str.replace(​reg,'$1'));​​​​​​​​​​​​​​​​​​​​​​​​
I get the following output : holes[0
I don't understand how my first group is supposed to represent "holes[0", since I included the whole [shots][ part in it.
I appreciate any inputs on this. THank you.
In strings, a single \ is not interpreted as a Regex-escaping character. To escape the bracket within string literals, you have to use two backslashes, \\:
var reg = new RegExp("(.*\\[shots\\]\\[)([0-9]+)(\\].*\\])", "g");
A preferable solution is to use RegEx literals:
var reg = /(.*\[shots\]\[)([0-9]+)(\].*\])/g;
Looks like, this works:
var str = "holes[0][shots][0][units]";
var reg = /(.*\[shots\]\[)([0-9]+)(\].*\])/;
console.log(str.replace(​reg,'$1'));​​​​​​​​​​​​​​​​​​​​​​​​

Categories