regular expression with javascript for second match - javascript

I have an complicated string. From this string I like to get only the following marked part: (second match)
Example:
1;#FirstName,,
Surname,#Domain\Account,#email.#company.com,#email#company.com,#FirstName,,
Surname25;#FirstName,,
Surname,#Domain\Account,#email.#company.com,#email#company.com,#FirstName,, Surname26;#Helpdesk,#DE\helpdesk,#helpdesk#vega.com,#,#Helpdesk30;#...
I only want to get the Second "FirstName,,Surname" combination...
Any ideas how I could to this?
In the example above I need to ignore the complete first part starting from 1;# to 25;#
And then I need the "FirstName,,Surname" and after that the rest of the string should be ignored.
The numbers can be different and also the length from the string...
I started with this but it is not working:
((.*?[0-9]+.*?){2})[0-9]+
Thanks in advance for your help.

you can use string split function and split by var arry = yourstring.split(',') arry[9],arry[11] will contain your string.

Try the following regex:
(?:.+?;){2}#(.+?),#
(very weird string by the way)
You can see it working here: http://regex101.com/r/dV1lW5 and here: http://regexr.com?33rn5

Related

Replace params in javascript

I tried a lot to replace the query parammeter using Javascript. But its not working. Can you please share any solutions to replace the parameter
Below is the example
console.log("www.test.com?x=a".replace(new RegExp(`${"x=a"}&?`),''));
the output i am getting is www.test.com? . Is there any way to replace ? and to get only www.test.com.
If you want to remove whatever comes from the question mark including it, try this instead:
console.log("www.test.com?x=a".split("?")[0]);
That way you get only what's before the question mark.
I hope that helps you out.
You can remove all query strings using the following regex:
\?(.*)
const url = "www.test.com?x=1&b=2"
console.log(url.replace(/\?(.*)/, ''));
You could brutally replace the '?x=a' string with the JavaScript replace function or, even better, you could split the string in two (based on the index of ?) with the JavaScript split function and take the first part, e.g.:
let str = 'www.test.com?x=a';
console.log(str.replace('?x=a', ''));
console.log(str.split('?')[0]);

Regex - Match only two digitsafter substring

I'm trying to replace all elements like \u00XY in a string that can contain multiple entries like that.
'"\u00bfIdade del titular?"'
This can be a short string or a string containing json objects inside... (I know... but... old code)
I tried normalize after the string but it didn't work, so I got instructed to replace all of those elements in unicode with a '?' char.
Any ideas on a simple way for this purpose? I'm not being able to find the right regex for this.
[0-9]{1,2}[\w]{2}?
Try this or modify it.
I made a small function that replaces all the unicode.
function replace_unicode_escape_sequence($sting) {
//replace all \uxxxx for correct html equvilant
$decoded_string = mb_convert_encoding(pack('H*', $sting), 'UTF-8', 'UCS-2BE');
return $decoded_string;
}
Hope this wil help you!

What Regex would capture both the beginning and end from of a string?

I am trying to edit a DateTime string in typescript file.
The string in question is 02T13:18:43.000Z.
I want to trim the first three characters including the letter T from the beginning of a string AND also all 5 characters from the end of the string, that is Z000., including the dot character. Essentialy I want the result to look like this: 13:18:43.
From what I found the following pattern (^(.*?)T) can accomplish only the first part of the trim I require, that leaves the initial result like this: 13:18:43.000Z.
What kind of Regex pattern must I use to include the second part of the trim I have mentioned? I have tried to include the following block in the same pattern (Z000.)$ but of course it failed.
Thanks.
Any help would be appreciated.
There is no need to use regular expression in order to achieve that. You can simply use:
let value = '02T13:18:43.000Z';
let newValue = value.slice(3, -5);
console.log(newValue);
it will return 13:18:43, assumming that your string will always have the same pattern. According to the documentation slice method will substring from beginIndex to endIndex. endIndex is optional.
as I see you only need regex solution so does this pattern work?
(\d{2}:)+\d{2} or simply \d{2}:\d{2}:\d{2}
it searches much times for digit-digit-doubleDot combos and digit-digit-doubleDot at the end
the only disadvange is that it doesn't check whether say there are no minutes>59 and etc.
The main reason why I didn't include checking just because I kept in mind that you get your dates from sources where data that are stored are already valid, ex. database.
Solution
This should suffice to remove both the prefix from beginning to T and postfix from . to end:
/^.*T|\..*$/g
console.log(new Date().toISOString().replace(/^.*T|\..*$/g, ''))
See the visualization on debuggex
Explanation
The section ^.*T removes all characters up to and including the last encountered T in the string.
The section \..*$ removes all characters from the first encountered . to the end of the string.
The | in between coupled with the global g flag allows the regular expression to match both sections in the string, allowing .replace(..., '') to trim both simultaneously.

Javascript substring check using indexOf or search on a date string with forward slash /

I am surprised to not to find any post regarding this, I must be missing something very trivial. I have a small JavaScript function to check if a string matches an object's properties. Simple stuff right? It works easily with all strings except those which contain a forward slash.
"‎04‎/‎08‎/‎2015‎".indexOf('4') // returns 2 :good
"‎04‎/‎08‎/‎2015‎".indexOf('4/') // returns -1 :why?
The same issue appears to be with .search() function as well. I encountered this issue while working on date strings.
Please note that I don't want to use regex based solution for performance reasons. Thanks for your help in advance!
Your string has invisible Unicode characters in it. The "left-to-right mark" (hex 200E) appears around the two slash characters as well as at the beginning and the end of the string.
If you type the code in on your browser console instead of cutting and pasting, you'll see that it works as expected.

What's wrong with this regular expression to find URLs?

I'm working on a JavaScript to extract a URL from a Google search URL, like so:
http://www.google.com/search?client=safari&rls=en&q=thisisthepartiwanttofind.org&ie=UTF-8&oe=UTF-8
Right now, my code looks like this:
var checkForURL = /[\w\d](.org)/i;
var findTheURL = checkForURL.exec(theURL);
I've ran this through a couple regex testers and it seems to work, but in practice the string I get returned looks like this:
thisisthepartiwanttofind.org,.org
So where's that trailing ,.org coming from?
I know my pattern isn't super robust but please don't suggest better patterns to use. I'd really just like advice on what in particular I did wrong with this one. Thanks!
Remove the parentheses in the regex if you do not process the .org (unlikely since it is a literal). As per #Mark comment, add a + to match one or more characters of the class [\w\d]. Also, I would escape the dot:
var checkForURL = /[\w\d]+\.org/i;
What you're actually getting is an array of 2 results, the first being the whole match, the second - the group you defined by using parens (.org).
Compare with:
/([\w\d]+)\.org/.exec('thisistheurl.org')
→ ["thisistheurl.org", "thisistheurl"]
/[\w\d]+\.org/.exec('thisistheurl.org')
→ ["thisistheurl.org"]
/([\w\d]+)(\.org)/.exec('thisistheurl.org')
→ ["thisistheurl.org", "thisistheurl", ".org"]
The result of an .exec of a JS regex is an Array of strings, the first being the whole match and the subsequent representing groups that you defined by using parens. If there are no parens in the regex, there will only be one element in this array - the whole match.
You should escape .(DOT) in (.org) regex group or it matches any character. So your regex would become:
/[\w\d]+(\.org)/
To match the url in your example you can use something like this:
https?://([0-9a-zA-Z_.?=&\-]+/?)+
or something more accurate like this (you should choose the right regex according to your needs):
^https?://([0-9a-zA-Z_\-]+\.)+(com|org|net|WhatEverYouWant)(/[0-9a-zA-Z_\-?=&.]+)$

Categories