JavaScript regex to search between strings - javascript

Hi I would like to search between strings using regex for JavaScript
String format:
saobjectid=tt-234,ou=info
saobjectid=bb-456,ou=info
saobjectid=bng,ou=info
saobjectid=asx 1 ert 7,ou=info
Expected output:
tt-234
bb-456
bng
asx 1 ert 7
I have tried this
[a-z]+[-,\s]+[0-9]+
But didn't manage to capture all different scenarios
Thanks for your help

Just added this answer to depict that it can also be done using substr in javascript:
var a = "saobjectid=tt-234,ou=info";
var b = "saobjectid=bb-456,ou=info";
var c = "saobjectid=bng,ou=info";
var d = "saobjectid=asx 1 ert 7,ou=info";
console.log(getSubstr(a));
console.log(getSubstr(b));
console.log(getSubstr(c));
console.log(getSubstr(d));
function getSubstr(a){
return a.substr(a.indexOf('=')+1, a.indexOf(',') - a.indexOf('=')-1);
};

a regex like =([-\s\w]+), will do.
Check it out at regex101 or here:
var s=`saobjectid=tt-234,ou=info
saobjectid=bb-456,ou=info
saobjectid=bng,ou=info
saobjectid=asx 1 ert 7,ou=info"`;
var regexp = /=([-\s\w]+),/g;
while ((match = regexp.exec(s)) != null){
console.log(match[1]);
}

Related

Javascript and Regex tuning - striping string

From the URL:
https://www.flightstats.com/v2/historical-flight/TP/1478/2020/11/3/1047614176
I need to get "2020/11/3"
WHAT I HAVE
The Regex:
\d\/(\d+\/\d+\/\d+)
it returns: for full match - "8/2020/1/3", for Group 1 - "2020/1/3". I've tested several combinations and tried to simplify it till this version
The Javascript:
var myRe = /\d\/(\d+\/\d+\/\d+)/;
var myArray = myRe.exec('${initialurl}');
Being initialurl a variable
PROBLEMS
The javascript returns: "8/2020/11/3,2020/11/3" and I only need/want the group 1 match or if the full match is correct, just that.
CONTEXT
Javascript newbie
I'm using this in Ui.Vision Kantu
If your URLs are going to reliably be in the format shown then this would do it:
\d{4}\/\d{1,2}\/\d{1,2}
https://regex101.com/r/BqW2lr/1
var initialurl = 'https://www.flightstats.com/v2/historical-flight/TP/1478/2020/11/3/1047614176';
var myRe = /\d{4}\/\d{1,2}\/\d{1,2}/;
var myArray = myRe.exec(initialurl);
console.log(myArray);

Javascript regex, replacing numbers with manipulated number

I have the following string:
"4/7/12"
and I would like to replace each number with this formula:
(25 - x) where 'x' is the number from the string.
For example:
"4/7/12" would be translated into: "21/18/13"
How can I do this using 'replace()' and Regex ??
var player_move = "5/7/9";
var translated_pm = player_move.replace(/\/\*?/, 25 - /$1/);
Thank you!
Try this, all in one line:
var player_move = "5/7/9";
var new_move = player_move.split('/').map(function(number) { return 25 - Number(number); }).join('/');
alert(new_move);
Do you have to use a regex?
JsBin example
without regex
This might be a better way to do it:
var n = "4/7/12".split('/').map(function(el) {
return 25 - Number(el); // Number not needed here bc of coercion but I like it here
}).join('/');
regexp
With .replace, you can pass in a function like so:
var re = "4/7/12".replace(/\d+/g, function(match) {
return 25 - match;
})
Try this
var translated_pm = player_move.replace(/\d+/g, function (x){return 25 - parseInt(x)});

Ignore first character of string - JS - Regex

I'm trying to write a Regex that will ignore the first character of a string and start with the second character.
e.g.
str = "14";
test = "4";
This will match ONLY if 4 is is position 2 (end of the string) and NOT at the start, the following will fail
str = "21";
test = "4";
I'm rubbish at Regex and all the options I've tried so far haven't worked.
My current code is like so
filters = filters.replace(/,\s*$/, '');
objRegex = new RegExp('\\/^.{1}(.*)/' + filters, 'gi');
Where filters is a random string consisting of two characters. The current Regex was copied from another SO post but it doesn't work and given my limited knowledge I'm not sure how to make it work, anyone able to help?
Thanks!
I think a Regex is a bit overkill, how about something like this:
var stringToSearch = '14';
var stringToFind = '4';
if (stringToSearch && stringToSearch.length === 2 &&
stringToSearch[1] === stringToFind) {
// do something
}
Just use substring method ?
str = "14";
test = "4";
var str = str.substring(0, 2);
Use following pattern:
"^[\w]{1}4$"

Regex to capture whole word with specific beginning

I need to capture a number passed as appended integers to a CSS class. My regex is pretty weak, what I'm looking to do is pretty simple. I thought that "negative word boundary" \B was the flag I wanted but I guess I was wrong
string = "foo bar-15";
var theInteger = string.replace('/bar\-\B', ''); // expected result = 15
Use a capture group as outlined here:
var str= "foo bar-15";
var regex = /bar-(\d+)/;
var theInteger = str.match(regex) ? str.match(regex)[1] : null;
Then you can just do an if (theInteger) wherever you need to use it
Try this:
var theInteger = string.match(/\d+/g).join('')
string = "foo bar-15";
var theInteger = /bar-(\d+)/.exec(string)[1]
theInteger // = 15
If you just want the digits at the end (a kind of reverse parseInt), why not:
var num = 'foo bar-15'.replace(/.*\D+(\d+)$/,'$1');
or
var m = 'foo bar-15'.match(/\d+$/);
var num = m? m[0] : '';

split javascript string to get desired values

I want to extract the date and the username from string using .split() in this particular string:
var str ='XxSPMxX on 08/30/2012';
I want XxSPMxX in one variable and 08/30/2012 in the other.
Using just split:
var x = str.split('</a> on ');
var name = x[0].split('>')[1];
var date = x[1];
Demo: http://jsfiddle.net/Guffa/YUaAT/
I don't think split is the right tool for this job. Try this regex:
var str ='XxSPMxX on 08/30/2012',
name = str.match(/[^><]+(?=<)/)[0],
date = str.match(/\d{2}\/\d{2}\/\d{4}/)[0];
Here's the fiddle: http://jsfiddle.net/5ve7Y/
Another way would be to match using a regular expression, build up a small array to get the parts of the anchor, and then use substring to grab the date.
var str = 'XxSPMxX on 08/30/2012';
var matches = [];
str.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function () {
matches.push(Array.prototype.slice.call(arguments, 1, 4))
});
var anchorText = matches[0][2];
var theDate = str.substring(str.length - 10, str.length);
console.log(anchorText, theDate);
working example here: http://jsfiddle.net/dkA6D/

Categories