This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I need a Regular Expression which can meet my request below:
It can get characters between 'id=' and '&'
the character '&' may exist or not
here's a example, say I have two URL like below:
http://example.com?id=222&haha=555
http://example.com?id=222
The Regular Expression can get the key id's value 222 in both URL.
VERY THANKFUL if someone can help me solve this problem!!!
You could try the below regex to get the id value whether it is followed by a & symbol or line end $.
id=(.*?)(?=&|$)
The captured value was stored inside the group index 1.
DEMO
> var re = /id=(.*?)(?=&|$)/g;
undefined
> var str = 'http://example.com?id=222&haha=555';
undefined
> var m;
undefined
> while ((m = re.exec(str)) != null) {
... console.log(m[1]);
... }
222
Not the regexp way but this should also work
str.split("id=")[1].split('&')[0]
Related
This question already has answers here:
Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms
(10 answers)
XML parsing of a variable string in JavaScript
(10 answers)
The best node module for XML parsing [closed]
(2 answers)
Closed 4 years ago.
I have a string as follows:
<abc name = "foo">
<child>bar</child>
</abc>
<xyz>1</xyz>
<abc name = "foo2">
<child>bar2</child>
</abc>
<xyz>5</xyz>
I have created a regex as follows:
var regexapi = /<abc\s*name\s*=\s*"(.*?)"[\s\S]*?<\/abc>\n*<xyz>/gim;
while ( (resApi = regexapi.exec(data))) {
array1.push(resApi[0]);
}
console.log(array1[0]);
Now if I don't have the tag <xyz>1</xyz> printing array1[0] should show undefined but it is printing as follows:
<abc name = "foo">
<child>bar</child>
</abc>
<abc name = "foo2">
<child>bar2</child>
</abc>
<xyz>
I think there is some problem in \n* since I'm giving multiline flag. Not sure aout this though.
Note that this is without <xyz>1</xyz> tag. I want it to print undefined.
Thanks.
Regex:
<\/abc>\n(?:<xyz>(.*)(?=<\/xyz))*
Regex Demo
js Demo
Matches a </abc> followed by <xyz> and value. if <xyz> tag is missing array[0] will return an empty string (not undefined)
You would be better off using an XML parser here. If you insist on using regex, here is one option:
var input = "<abc name = \"foo\">\n\t<child>bar</child>\n</abc>\n<xyz>\n\n<abc name = \"foo2\">\t\n<child>bar2</child>\n</abc>\n<xyz>35</xyz>";
var regex = /<abc[^>]*>(?:(?!<\/abc>)[\s\S]*)<\/abc>\s*<xyz>((?!<xyz>)[\s\S]*)<\/xyz>/g;
var match = regex.exec(input);
console.log(match[1]); // 35
This matches an <abc> tag followed by optional whitespace, then followed immediately by an <xyz> tag. Should that tag be empty, then nothing would be capture in the first capture group match[1].
This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
Working in JavaScript I have strings that might contain words prefixed by an "#". The string might look like this #one two #three. I want a RegExp that finds the words prefixed by the "#" but I don't want to include the "#" itself.
I created a RegExp looking like this /#(\w+)/g but when I match it against my strings the "#" is indeed included. Why is this? I assumed that if I wanted to include it then the "#" would be inside the capturing parentheses like this /(#\w+)/g
var s = '#one two #three'
var matches = s.match(/#(\w+)/g)
// matches is now [ '#one', '#three' ] but I want [ 'one', 'three' ]
Note with the result I currently get, there is of course no problem in getting the array I want, I can just remove the first character of each string. But what I want to know is:
Is it possible to change my RegExp to get the desired result?
And:
Why are characters outside my capturing parenthesis included in the result? I find it intuitive that only the characters inside the parenthesis should be included.
You need to access first capturing group
var re = /#(\w+)/gm;
var str = '#one two #three';
var m;
while ((m = re.exec(str)) !== null) {
print(m[1]);
}
Ideone Demo
JS Demo
var re = /#(\w+)/;
var arr = ['#one', 'two', '#three'];
arr.forEach(function(str) {
if (str.match(re))
document.writeln(str.match(re)[1] + '<br>');
});
This question already has answers here:
Regular Expressions: Is there an AND operator?
(14 answers)
Closed 7 years ago.
I have a simple filter function in my javascript, based on an input box.
function filter(selector, query) {
query = $.trim(query); //trim white space
query = query.replace(/ /gi, '|'); //add OR for regex
$(selector).each(function() {
($(this).text().search(new RegExp(query, "i")) < 0) ? (do something here)
So, if I have a table with a list of words, eg.
Alpha Centauri,
Beta Carbonate,
Charly Brown,
...
and I enter 'alpha cen' into my input box, the function searches for ('alpha' or 'cen') and returns the one record as desired.
However, if I replace the '|' with a '&' to search for ('alpha' and 'cen') I get no results. Even if I enter 'alpha centauri', I get no result at all.
Why?
While a | in a regex allows alternation, an & carries no special meaning. Your current code is trying to find a literal match for alpha&cen, which clearly doesn't match any of your data.
This question already has answers here:
JavaScript query string [closed]
(15 answers)
Closed 7 years ago.
I have a url
http://localhost:8162/UI/UsersDetail.aspx?id_temp=U0001
I want to get string use javascript
?id_temp=U0001
Thank guys.
If this isn't the location of the page, you may use
var str = url.match(/\?.*$/)[0];
If this url is the current one of your page, use
var str = location.search;
You can use regex:
url.match(/\?(\w+=\w+)/)[0];
/ : Delimiter of regex
\? : Matches ? need to escape using \
\w+: Matches all alphanumeric characters and _
= : Matches =
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I need javascript regular expression to check string starting with U or C. Also it should be of length 10.
I tried this ^U|C{9}$ but not getting proper results.
Use this regex instead:
^[UC].{9}$
The . will match any character, which you missed out.
You should group the tokens when using | operator, so as to get intended results. Also, you must use .(any character except new-line) and make sure it is 9 characters long.
You can also use character class as in /^[UC].{9}$/
/^(U|C).{9}$/
You can also use simple javascript to do this
var chr = str.charAt(0);
if((chr == "U" || chr == "C") && str.length == 10){
// valid
}