I have the following types of query string. It could be anyone of the below.
index.html?cr=33&m=prod&p=ded
index.html?cr=33&m=prod&p=ded&c=ddl&h=33&mj=ori
From the above query string, i wanted to extract only m & p "m=prod&p=ded".
Otherway to do is split and get, that i have already done it.
But I wanted to achieve it using regular expression.
Any help is highly appreciated.
You can use something like this :
(?<=&)m=[^& ]*\&p=[^& ]*
In javascript , It doesn't support ?<= positive Lookbehind.
So you can use the following code ( in javascript ):
var s="index.html?cr=33&m=prod&p=ded"
s.match(/m=[^& ]*\&p=[^& ]*/g)
Refer Regexp101
If both query parameters come one after another then you you can use:
url.match(/[?&]m=([^&]*)&p=([^&]*)/i);
Related
I have a regex validation I need my string to pass.
/^[0-9a-zA-Z-]+$/
I want to create a function that sanitizes the string for it to pass the regex.
I thought of doing something like
string.replace(/^[0-9a-zA-Z-]+$/,"");
Except I need to invert the above regex.
I tried to look up how to invert a regex but nothing seems to show up.
Try this string.replace(/\W/g,""). Also check this web site i always use it to test regular expressions, it also has hints on the right bottom
Negate the collection using ^ inside the []
const str = `abc*รง%ABC&(/())12345=?`
const newString = str.replace(/[^0-9a-zA-Z-]/g,"");
console.log(newString)
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
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_\-?=&.]+)$
i'm having problem when i want to use regexp, i don't know to using 'or' , 'and'. like in if statement.
i'm having url like this
http://localhost:85/study/list
and i'm having the words(pattern to regexp) /,/list/top
when i try using this
url ="http://localhost:85/study/list";
regexp = RegExp(.*/?/list?/top);
var matches = url.match(regexp);
alert(matches);
and when i change url to http://localhost:85/top the word of /top not detection but /list is detection . what is wrong?
i wanna to match my words in that url , how i must create regexp for that?
please tell me your answer and please give me reference for the regexp
i'm newbie thanks for helping...
Man, you can test your expressions here RegexPal. It seems that the expression you using havo no effects.
This expression will match the last sub-directory of your url
(\w*)$
For example http://localhost:85/study/list will match list, http://localhost:85/top will match just top.
Try it! =)
Put them in parenthesis, such as (/list)?.
list? matches lis or list. I think you want (list)?
I am trying to find a way to check if a string contains a specific sequence of characters in JScript.
In my case, I am trying to see if the string is "DPObject" followed by a number. Such as "DPObject3" or "DPObject14".
Thank you!
if (/DPObject\d+/.test(string)) {....}
Javascript String has an indexOf method you can use to check if a String contains a particular substring .
If you need to test for patterns , like "DPObject" followed by an integer , probably you need to use Regexes . ( http://www.regular-expressions.info )
It's javascript , or js for short - not JScript .
Then you should use a regular expression. I think this would be something like :
var re = new RegExp("^DPObject([0-9]+)$");
re.test(someString);
This ensures there is at least only one digit after DPObject.
The "^" at the beginning is to ensure the string starts with DPObject. Check references on regexps for this kind of problems :)
edit: added "$" to mark the end of the string, the updated should be more "solid"
There are a couple of ways:
Use Javascripts indexOf method
Use Javascript Regular Expressions
Use JQuery's contains function
Regular expressions are the most powerful and elegant way of doing it. They syntax makes sense after a while (honestly). ;-)
Good luck.