Losing the expression in debugger - javascript

I am trying to get a regular expression to remove some junk on a string that I am getting back from a service. The string has some phone data that I want to remove. An example string is "en-us; droid4 build/9.8.2o-72_vzw-18" . My first task is to drop the "build/" to the end of the string or to the end of word (not sure which is better in my scenario).
I ended up with:
var deviceRegExpr = new RegExp("\b(build\/\S*)");
First off, I am not sure if this is the best way to accomplish this. But, when I am looking at the Chrome debugger tools the regExp value is showing up as "/(build/S*)/" . I cant figure out what is happening to the \b ?
Thanks in advance for any help.

This String#replace(regex, repacement) should work:
var repl = "en-us; droid4 build/9.8.2o-72_vzw-18".replace(/\s*\bbuild\/.*$/, "");
//=> en-us; droid4

Related

Regex expression to match certain url behavior in my website

I have the following url
https://myurl/blogs/<blog-category>/<blog-article>
I've trying to create a regEx so i can thrigger a script only when i'm in an article.
i tried this among other tests but it didn't work and i'm not really the best guy building RegExs.
window.location.pathname.match(/\/blogs\/^[a-zA-Z0-9_.-]*$\/^[a-zA-Z0-9_.-]*$/
So in my understanding the first part of this regEx (\/blogs\/) is trying just to match a fixed string.
Then next parts just tries to match any kind of numeric,character and _.- combination (which is basically the potential strings that i can have there)
However this is not working at all.
My piece of script is looking like this
if(window.location.pathname.match(/\/blogs\/^[a-zA-Z0-9_.-]*$\/^[a-zA-Z0-9_.-]*$/){
// A code implementation here
}
Note: One thing that i noticed when writing this is that if i remove everything and just try
window.location.pathname.match(/\/blogs\/)
It doesn't work either.
Can someone help me solve this? I will also appreciate any guide that can help me improve my RegEx skills.
Thanks!
Update: to have this working i had to separate my condition into two things to get it to work properly.
It ended up looking like this:
var path = window.location.pathname;
const regEx = /\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*/i;
if(path.match(regEx)){
// My code here
}
This should work:
\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*
the "^" symbol checks that it is the start of a string which is not the case for the url in question
I would suggest using https://regexr.com/ for testing your regex to remove any other possible issues from other code
var patt = /\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*/i window.location.pathname.match(patt)
You can try using this

Regex to verify and parse path on location.hash

This problem looks simple at first, but I'm struggling to make it work.
I know that I can achieve the result I am looking for with urlsplit() and if, but my doubt is about how to make this with regex, because:
I can check the start of the string
I can get the attributes that I need
All in a single line, and much more beautifull than using urlsplit() and if
So the problem is, at some point I will have this on my location.hash:
var h = "#/myapp/arg1/arg2/arg3"
First I need to check if it starts with "#/myapp", and if it does, I need the rest of the path in a list like ['arg1','arg2','arg3']
So far I did this:
var rex = /#\/?myapp\/?(([^\/]+)?[\/$])*/;
rex.exec(h);
> ["#/myapp/arg1/arg2/", "arg2/", "arg2"]
What I am doing wrong?
How do I create a regex to make this happen?
Try:
"#/myapp/arg1/arg2/arg3".split("/").slice(2);
Or remove the first part first:
"#/myapp/arg1/arg2/arg3".substring("#/myapp".length+1).split("/");
Or with regex:
"#/myapp/arg1/arg2/arg3".match(/[^/]+/g).slice(2);

JavaScript Regex Replacement with Reference

I'm struggling a little bit with a JavaScript regex statement - and I can't quite see what's wrong. I've tested in online tools and they suggest it should work so I'm assuming there's something different between C# regex that I'm used to and JavaScript.
The string I'm working with is quite simple:
[a] + [b]
The regex match I'm trying to use is:
/[(?<name>[a-zA-Z0-9])/]
I'm trying to replace the value with the following:
viewModel.$1.control.value()
Which should leave me with:
viewModel.a.control.value() + viewModel.b.control.value()
Unfortunately I'm always getting my inital value printed, suggesting my matching isn't working but I can't see why. The only obvious thing I tried was switching the escaping of the square brackets between forward and backslash.
Can anyone suggest what else might be wrong?
There is no named groups in Javascript regex. Use this:
var s = '[a] + [b]';
repl = s.replace(/\[([a-zA-Z0-9])\]/g, 'viewModel.$1.control.value()');
//=> "viewModel.a.control.value() + viewModel.b.control.value()"
Also you need to escape [ and ] in order to match them in a regex.

javascript regex invalid quantifier error

I have the following javascript code:
if (url.match(/?rows.*?(?=\&)|.*/g)){
urlset= url.replace(/?rows.*?(?=\&)|.*/g,"rows="+document.getElementById('rowcount').value);
}else{
urlset= url+"&rows="+document.getElementById('rowcount').value;
}
I get the error invalid quantifier at the /?rows.*?.... This same regex works when testing it on http://www.pagecolumn.com/tool/regtest.htm using the test string
?srt=acc_pay&showfileCL=yes&shownotaryCL=yes&showclientCL=no&showborrowerCL=yes&shownotaryStatusCL=yes&showclientStatusCL=yes&showbillCL=yes&showfeeCL=yes&showtotalCL=yes&dir=asc&closingDate=12/01/2011&closingDate2=12/31/2011&sort=notaryname&pageno=0&rows=anything&Start=0','bodytable','xyz')
In this string, the above regex is supposed to match:
rows=anything
I actually don't even need the /? to get it to work, but if I don't put that into my javascript, it acts like it's not even regex... I'm terrible with Regex period, so this one has me pretty confused. And that error is the only one I am getting in Firefox's error console.
EDIT
Using that link I posted above, it seems that the leading / tries to match an actual forward slash instead of just marking the code as the beginning of a regex statement. So the ? is in there so that if it doesn't match the / to anything, it continues anyway.
RESOLUTION
Ok, so in the end, I had to change my regex to this:
/rows=.*(?=\&?)/g
This matched the word "rows=" followed by anything until it hit an ampersand or ran out of text.
You need to escape the first ?, since it has special meaning in a regex.
/\?rows.*?(?=\&)|.*/g
// ^---escaped
regtest.htm produces
new RegExp("?rows.?(?=\&)|.", "") returned a SyntaxError: invalid
quantifier
The value you put into the web site shouldn't have the / delimiters on the regex, so put in ?rows.*?(?=\&)|.* and it shows the same problem. Your JavaScript code should look like
re = /rows.*?(?=\&)|.*/g;
or similar (but that is a pointless regex as it matches everything). If you can't fix it, please describe what you want to match and show your JavaScript
You might consider refactoring you code to look something like this:
var url = "sort=notaryname&pageno=0&rows=anything&Start=0"
var rowCount = "foobar";
if (/[\?\&]rows=/.test(url))
{
url = url.replace(/([\?\&]rows=)[^\&]+/g,"$1"+rowCount);
}
console.log(url);
Output
sort=notaryname&pageno=0&rows=foobar&Start=0

Grabbing the third fragment between square brackets

Still completely stuck with regex's and square brackets. Hopefully someone can help me out.
Say I have a string like this:
room_request[1][1][2011-08-21]
How would I grab the third fragment out of it?
I tried the following, but I'm not exactly sure what I'm doing so it's fairly hard to figure out where I'm going wrong.
.match(/\[(.*?)\]/);
But this returns the [1] fragment. (The first one, I guess).
So then, I asked here on SO and people told me to add a global flag:
.match(/\[(.*?)\]/g)[2];
In other cases that I've used this regex, this worked fine. However, in this case, I want the stuff INSIDE the square brackets. It returns:
[2011-08-21]
But I really want 2011-08-21.
How can I do this? Thanks a lot.
If anyone could recommend any decent resources about regular expressions, that'd be great aswell. I'm starting to understand the very basics but most of this stuff is far too confusing atm. Thanks.
Two possible methods. To grab the third bracketed expression:
.match(/\[.*?\]\[.*?\]\[(.*?)\]/);
Or, if you know that the expression you want is always at the end of the string:
.match(/\[(.*?)\]$/);
var str = "room_request[1][1][2011-08-21]"
var val = str.match(/\[[^\]]*\]\[[^\]]*\]\[([^\]]*)\]/);
alert(val[1]);
This is a little less messy I think:
var r = "room_request[1][1][2011-08-21]";
var match = r.match(/(?:\[([^\]]+)\]){3}/);
console.log(match[1]);
Basically, it picks out the third match of the square brackets containing something. You get the match result back with two matches - the whole [1][1][2011-08-21] (for whatever reason) and the matched date: 2011-08-21
My regex is a little rusty, but this certainly works.

Categories

Resources