Regex match whole expression instead of 2 matches [duplicate] - javascript

I have the text:
s.events="event3"
s.pageName="Forum: Index"
s.channel="forum"
s.prop1="Forum: Index"
s.prop2="Index Page"
s.prop36=""
s.prop37=""
s.prop38=""
s.prop39=""
s.prop40="53"
s.prop41="Anonymous"
s.prop42="username"
s.prop43=""
s.prop47=""
s.eVar1="Forum: Index"
s.eVar2="Index Page"
s.eVar36=""
s.eVar37=""
saved in a var in javascript and I want to extract the text between the quotes of s.prop42 giving me the result:
"username"
what I have right now is
var regex = /\?prop.42="([^']+)"/;
var test = data.match(regex);
but it doesnt seem to work, can someone help me out?

Use this:
var myregex = /s\.prop42="([^"]*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[1];
}
In the regex demo, look at the capture group in the right pane.
Explanation
s\.prop42=" matches s.prop42=" (but we won't retrieve it)
The parentheses in ([^"]*) capture any chars that are not a " to Group 1: this is what we want
The code gets the Group 1 capture

Can't comment on above answer, but I think the regex is better with .* like so:
var myregex = /s\.prop42="(.*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[1];
}

Related

Finding multiple groups in one string

Figure the following string, it's a list of html a separated by commas. How to get a list of {href,title} that are between 'start' and 'end'?
not thisstartfoo, barendnot this
The following regex give only the last iteration of a.
/start((?:<a href="(?<href>.*?)" title="(?<title>.*?)">.*?<\/a>(?:, )?)+)end/g
How to have all the list?
This should give you what you need.
https://regex101.com/r/isYIeR/1
/(?:start)*(?:<a href=(?<href>.*?)\s+title=(?<title>.*?)>.*?<\/a>)+(?:,|end)
UPDATE
This does not meet the requirement.
The Returned Value for a Given Group is the Last One Captured
I do not think this can be done in one regex match. Here is a javascript solution with 2 regex matches to get a list of {href, title}
var sample='startfoo, bar,barendstart<img> something end\n' +
'beginfoo, bar,barend\n'+
'startfoo again, bar again,bar2 againend';
var reg = /start((?:\s*<a href=.*?\s+title=.*?>.*?<\/a>,?)+)end/gi;
var regex2 = /href=(?<href>.*?)\s+title=(?<title>.*?)>/gi;
var step1, step2 ;
var hrefList = [];
while( (step1 = reg.exec(sample)) !== null) {
while((step2 = regex2.exec(step1[1])) !== null) {
hrefList.push({href:step2.groups["href"], title:step2.groups["title"]});
}
}
console.log(hrefList);
If the format is constant - ie only href and title for each tag, you can use this regex to find a string which is not "", and has " and a space or < after it using lookahead (regex101):
const str = 'startfoo, barend';
const result = str.match(/[^"]+(?="[\s>])/gi);
console.log(result);
This regex:
<.*?>
removes all html tags
so for example
<h1>1. This is a title </h1><ul><a href='www.google.com'>2. Click here </a></ul>
After using regex you will get:
1. This is a title 2. Click here
Not sure if this answers your question though.

JS What's the fastest way to display one specific line of a list?

In my Javascript code, I get one very long line as a string.
This one line only has around 65'000 letters. Example:
config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...
What I have to do is replace all & with an break (\n) first and then pick only the line which starts with "path_of_code=". This line I have to write in a variable.
The part with replace & with an break (\n) I already get it, but the second task I didn't.
var obj = document.getElementById('div_content');
var contentJS= obj.value;
var splittedResult;
splittedResult = contentJS.replace(/&/g, '\n');
What is the fastest way to do it? Please note, the list is usually very long.
It sounds like you want to extract the text after &path_of_code= up until either the end of the string or the next &. That's easily done with a regular expression using a capture group, then using the value of that capture group:
var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
var text = match[1];
}
Live Example:
var theString = "config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...";
var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
var text = match[1];
console.log(text);
}
Use combination of String.indexOf() and String.substr()
var contentJS= "123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...";
var index = contentJS.indexOf("&path_of_code"),
substr = contentJS.substr(index+1),
res = substr.substr(0, substr.indexOf("&"));
console.log(res)
but the second task I didn't.
You can use filter() and startsWith()
splittedResult = splittedResult.filter(i => i.startsWith('path_of_code='));

Extract alphanumeric words starting with letter, ignore others

Sounds easy enough, but I am just unable to figure that out.
/([a-z][a-z0-9]+)/gi
Test case looks like this
Correct #123 2baZ #1a2 thisToo $bar andTwo2
I am fighting with those partial matches. Only valid should be: ["Correct", "thisToo", "andTwo2"]. Any others should stay unmatched.
Here is link to tester: http://regex101.com/r/qG7lU9/8
Update:
Here is JS fiddle that works better than tester itself... http://jsfiddle.net/FredyCr/6hsgef82/
You can use lookahead and non-capturing group based regex like this:
(?:^| )([a-z][a-z0-9]+(?= |$))
And use captured group #1 for your matches that gives:
Correct
thisToo
andTwo2
RegEx Demo
Code:
var rx = /(?:^| )([a-z][a-z0-9]+(?= |$))/gi
var str = " Correct #123 2baZ #1a2 thisToo $bar andTwo2";
var matches = [];
while (match = rx.exec(str))
matches.push(match[1]);
console.log(matches);
//=> ["Correct", "thisToo", "andTwo2"]
JsFiddle Demo
Get the matched strings from group index 2.
(?:^| )([a-z][a-z0-9]+)(?: |$)
DEMO
Javascript code would be,
> var re = /(?:^| )([a-z][a-z0-9]+)(?: |$)/gi
undefined
> var str = " Correct #123 2baZ #1a2 thisToo $bar andTwo2";
undefined
> var matches = [];
undefined
> while (match = re.exec(str))
... {
... matches.push(match[1]);
... }
3
> console.log(matches);
[ 'Correct', 'thisToo', 'andTwo2' ]
var regex = /(^|\s)[a-z][a-z0-9]+/gi;
var text = "Correct #123 2baZ #1a2 thisToo $bar andTwo2";
var found;
while ((found = regex.exec(text)) !== null)
console.log(found[0].trim());
Output
Correct
thisToo
andTwo2
Workaround: split and filter the tokens you want:
var rx = /^[a-zA-Z][a-zA-Z0-9]*$/;
var match = str.split(/\s+/).filter(rx.exec.bind(rx));
Example

My regular expression isn't matching the correct group

I have a string:
var str = ' not valid xml here <something unknown>123</something>\
<something hello>555</something>\
<something what>655</something>';
var matches = str.match(/something[^>]+>([^<]+)/g);
I want matches to equal [123, 555, 655] and I thought () around my regex indicated this, but for some reason matches equals ["something unknown>123", "something hello>555", "something what>655"]. My solution was to do
matches.map(function(data){ return data.split('>').pop() })
but I was wondering if there's a more elegant way to do this by directly editing the regex, and I was wondering why () did not work.
Macmee, with all the usual disclaimers about parsing xml in regex, here is a simple regex that captures what you want:
<[^>]*>([^<]*)<\/
See the online demo (you are looking for the Group 1 captures in the bottom right pane).
Make sure you use g to get all the captures—but you already know that.
Here is also a full code demo for the code below.
<script>
var subject = '<something unknown>123</something>\
<something hello>555</something>\
<something what>655</something>';
var regex = /<[^>]*>([^<]*)<\//g;
var group1Caps = [];
var match = regex.exec(subject);
// put Group 1 captures in an array
while (match != null) {
if( match[1] != null ) group1Caps.push(match[1]);
match = regex.exec(subject);
}
document.write("<br>*** Matches ***<br>");
if (group1Caps.length > 0) {
for (key in group1Caps) document.write(group1Caps[key],"<br>");
}
</script>

java script regex .match find only one result

i have this js code :
result = subject.match(/<a.*class="gallery_browser_thumbnail".*href="(.+)">/i);
i want to get href of multiple a tags on a html source
but it shows only 1 result
if i use /g at end of pattern it returns whole patterns but i just want only the href part
i mean -> (.+) this part
this is how i capture html input :
var subject = String(document
.getElementsByTagName("body")[0].innerHTML);
any help?
final working script :
var subject = String(document.getElementsByTagName("body")[0].innerHTML);
var regex = /<a.*class="gallery_browser_thumbnail".*href="(.+)">/gi;
var matched = null;
while (matched = regex.exec(subject)) {
alert(matched[1]);
}
Change to lazy match by adding the lazy quantifier ?:
result = subject.match(/<a.*?class="gallery_browser_thumbnail".*?href="(.+?)">/i);
You can use exec to test RegExp. Something like this:
var subject = String(document.getElementsByTagName("body")[0].innerHTML),
regexp = /<a.*class="gallery_browser_thumbnail".*href="(.+)">/gi, //g for global match
match = regexp.exec(subject),
result = [];
while(match != null){
result.push(match[1]); // the second value is matched group
match = regexp.exec(subject);
}

Categories

Resources