Regular Expression to replace part of a string - javascript

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

Related

How to remove timestamp using regex from a date string in javascript

New to regex.
Need regular expression to match strings like T17:44:24Z from parent strings like 2015-12-22T17:44:24Z using javascript regex.
Once match is found will replace it with null or empty space.
To be specific i am trying to remove the time stamp from date string and obtain only date part.
Please help me in this
You can use a simple regex like this:
T\d{2}:\d{2}:\d{2}Z
or
T(?:\d{2}:){2}\d{2}Z
Working demo
In case your T and Z are dynamic the, you can use:
[A-Z]\d{2}:\d{2}:\d{2}[A-Z]
Code
var re = /T\d{2}:\d{2}:\d{2}Z/g;
var str = '2015-12-22T17:44:24Z';
var subst = '';
var result = str.replace(re, subst);
You don't need regex on this. You just split the string by T and get the second element from array, which would be 17:44:24Z in your case.
var date = '2015-12-22T17:44:24Z';
var result = date.split('T')[1];
If you also want to preserve T, you can just prepend it to the result:
var result = 'T' + date.split('T')[1]

JavaScript - strip everything before and including a character

I am relatively new to RegEx and am trying to achieve something which I think may be quite simple for someone more experienced than I.
I would like to construct a snippet in JavaScript which will take an input and strip anything before and including a specific character - in this case, an underscore.
Thus 0_test, 1_anotherTest, 2_someOtherTest would become test, anotherTest and someOtherTest, respectively.
Thanks in advance!
You can use the following regex (which can only be great if your special character is not known, see Alex's solution for just _):
^[^_]*_
Explanation:
^ - Beginning of a string
[^_]* - Any number of characters other than _
_ - Underscore
And replace with empty string.
var re = /^[^_]*_/;
var str = '1_anotherTest';
var subst = '';
document.getElementById("res").innerHTML = result = str.replace(re, subst);
<div id="res"/>
If you have to match before a digit, and you do not know which digit it can be, then the regex way is better (with the /^[^0-9]*[0-9]/ or /^\D*\d/ regex).
Simply read from its position to the end:
var str = "2_someOtherTest";
var res = str.substr(str.indexOf('_') + 1);

Javascript regex conditional statement when two of same character detected in string

I am trying to find a regular expression that detects when a string has two periods. When this condition has been met, it deletes all the characters up to (and including) the first period.
string:
"abc.def.ghi"
abc. would be removed, and we have:
"def.ghi"
I've recently learned I can't use conditionals in javascript regex. Is there a solution in regular javascript?
Regex code I have so far
/\.[^.]*\.?/
^[^.]*\.(?=[^.]*\.)
Try this.Replace by empty string.See demo.
https://regex101.com/r/sH8aR8/36
var re = /^[^.]*\.(?=[^.]*\.)/;
var str = 'abc.def.ghi\n\n';
var subst = '';
var result = str.replace(re, subst);
Try this:
str = "abc.def.ghi"
newStr = str.replace(/^[^.]*\.(.*?\.)/, '$1') // def.ghi
Demo

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'));​​​​​​​​​​​​​​​​​​​​​​​​

Javascript search and replace sequence of characters that contain square brackets

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\\]";.

Categories