Regex/Javascript Help - Search URL term Parsing - javascript

I am building a 'keyword' highlighting script and I need to write a regular expression to parse the following url, to find the searched for keywords to highlight.
I am REALLY bad with regex, so I was hoping someone who rocks it, could help.
Our search string uses "skw" as the parameter and "%2c" (comma) to separate terms, with "+" for spaces.
Example URLS:
http://[url].com/Search.aspx?skw=term1
http://[url].com/Search.aspx?skw=term1%2c+term2
Is there a single RegExp that I can use that will give me a collection that looks like this?
var "http://[url].com/Search.aspx?skw=term+one%2c+term2".match([Expression]);
matches[0] = "term one"
matches[1] = "term2"
Thanks! Any help is greatly appreciated.

You can't do this with a single match, but you can do it with a matches, a replace and a split:
url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ").split('%2c')
You may want to do the match separately and bail out if the match fails (which it could if yur URL didn't have an skw parameter).
You probably really want to do an unescape too, to handle other escaped characters in the query:
unescape(url.match(/\?(?:.*&)?skw=([^&]*)/)[1].replace(/\+/g, " ")).split(',')

https?://[^\?]+\?skw=([^%]*)(?:%2c\+*(.*))?
In javascript this is
var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^%]*)(?:%2c\+*(.*))?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
var term1 = match[1];
var term2 = match[2];
}
EDIT:
Sorry, I re-read your question, to handle multiple terms you need to combine this with a split
var subject = "http://[url].com/Search.aspx?skw=term1+one%2c+term2";
var myregexp = /https?:\/\/[^\?]+(?:\?|\?[^&]*&)skw=([^&]*)?/;
var match = myregexp.exec(subject);
if (match != null && match.length > 1) {
var terms = unescape(match[1]).split(",");
}

This task doesn't really lend itself to a single regular expression. Check out Parsing Query Strings in JavaScript for a script to assist you.

Related

Regex for not allowing special characters and allowing only single '&' not followed by another '&'

I have a existing regex for a string as below
const regEx = /^[^\]\\[!"\\$%'\\*\\+,\\./;<=>\\?#\\^`{\\|}~]*$/;
Now I want to allow & (which is working) but I don't want to allow consecutive '&' (like && or &&&, &&&&... should not be allowed). I am failing to combine these regex check together.
Can somebody help me on this please.
I tried (?![&]{2}) which did not work
Edit:
I have added another regex and validated separately which is now working. Anybody can help me to combine these two would be great.
const regEx1 = /^[^\]\\[!"\\$%'\\*\\+,\\./;<=>\\?#\\^`{\\|}~]*$/;
const regEx2 = /&(?=&)/;
return regEx1.test(value) && !regEx2.test(value);
The allowed string should be
Me & You
agdfhafdshfk
sfdsjg_dfjhk
3245rfgjh
(my name)
Not allowed
$%^*
+
me && you
me &&& you

Javascript Regular Expressions Capture

I need to capture this: <!.This is a string.!>
It can contain anything within the <!. .!>
So I tried this: <\!\..+\.\!>/g
Which didn't work. I've been trying to fix this for days now with no success. I want to capture the whole string including the <!. .!>
Thanks for the help!
EDIT
Since the OP edited his post to specify that he doesn't just want to capture the string but also the delimiters, I have moved out the capturing parentheses from <!\.(.*?)\.!> to (<!\..*?\.!>)
This should do it:
var myregexp = /(<!\..*?\.!>)/m;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1];
} else {
result = "";
}
Since JS does not support lookbehinds, the idea is to match the whole string, including the delimiters, but to only capture "My String" into Group 1. Then we inspect Group 1.
For someone that's "pretty good at Regex", this one is a no brainier.
var subject = "<!.This is a string.!>";
subject = subject.match(/<!\.(.*?)\.!>/);
console.log(subject[1]);
http://jsfiddle.net/tuga/YzLnN/1/

Regex trying to match characters before and after symbol

I'm trying to match characters before and after a symbol, in a string.
string: budgets-closed
To match the characters before the sign -, I do: ^[a-z]+
And to match the other characters, I try: \-(\w+) but, the problem is that my result is: -closed instead of closed.
Any ideas, how to fix it?
Update
This is the piece of code, where I was trying to apply the regex http://jsfiddle.net/trDFh/1/
I repeat: It's not that I don't want to use split; it's just I was really curious, and wanted to see, how can it be done the regex way. Hacking into things spirit
Update2
Well, using substring is a solution as well: http://jsfiddle.net/trDFh/2/ and is the one I chosed to use, since the if in question, is actually an else if in a more complex if syntax, and the chosen solutions seems to be the most fitted for now.
Use exec():
var result=/([^-]+)-([^-]+)/.exec(string);
result is an array, with result[1] being the first captured string and result[2] being the second captured string.
Live demo: http://jsfiddle.net/Pqntk/
I think you'll have to match that. You can use grouping to get what you need, though.
var str = 'budgets-closed';
var matches = str.match( /([a-z]+)-([a-z]+)/ );
var before = matches[1];
var after = matches[2];
For that specific string, you could also use
var str = 'budgets-closed';
var before = str.match( /^\b[a-z]+/ )[0];
var after = str.match( /\b[a-z]+$/ )[0];
I'm sure there are better ways, but the above methods do work.
If the symbol is specifically -, then this should work:
\b([^-]+)-([^-]+)\b
You match a boundry, any "not -" characters, a - and then more "not -" characters until the next word boundry.
Also, there is no need to escape a hyphen, it only holds special properties when between two other characters inside a character class.
edit: And here is a jsfiddle that demonstrates it does work.

JavaScript Regex to check that the searched string is preceded by either nothing, or spaces

I have the following code:
var requestData = {};
var byPattern = /by=(\w+)/;
var value = byPattern.exec(stringToSearch);
if (value && value.length === 2)
requestData.by = value[1];
The first problem with this regex, was that if stringToSearch is for example "standby=foo", it matched and returned "foo". I want it to fail there. I want the regex to match only if there is nothing before "by", or spaces.
So I replaced by /^\s*by=(\w+)/
That's better, but I want the regex to match if stringToSearch is for example "city=paris by=foo". It should match and return "foo". Not the case here :/
Can someone help me fix the regex? Thanks a lot!
the magic word is \b:
var byPattern = /\bby=(\w+)/;
console.log(byPattern.exec("by=3"));
console.log(byPattern.exec(" by=3"));
console.log(byPattern.exec("standby=3"));
Only the first two are matched, which is (I think) what you want.
If you want spaces or nothing before the capturing group, that would be ( +|^):
/( |^)by=(\w+)/
Technically, I'm matching space or nothing, but the effect is the same.

need help in finding javascript code

I am facing this problem. i am getting strings like this.
'=--satya','=---satya1','=-----satya2'.
now my problem is i have to remove these special characters and print the strings like this
'satya'
'satya1'
'satya2'
please help to solve this problem?
Use String.replace:
var s = '=---satya1';
s.replace(/[^a-zA-Z0-9]/g, '');
to replace all non-letter and non-number characters or
s.replace(/[-=]/g, '');
to remove all - and = characters or even
'=---satya-1=test'.replace(/(=\-+)/g, ''); // out: "satya-1=test"
to prevent removing further - or =.
You could extract that information with a regular expression such as
/\'\=-{0,}(satya[0-9]{0,})\'/
Live example: http://jsfiddle.net/LFZje/
The regex matches
Literal '
Literal =
Zero or more -
Starts a capture group and captures
- Literal satya
- Zero or more numbers
Ends the capture group
Literal '
Then using code such as
var regex = /\'\=-{0,}(satya[0-9]{0,})\'/g;
while( (match = regex.exec("'=--satya','=---satya1','=-----satya2'")) !== null)
{
// here match[0] is the entire capture
// and match[1] is tthe content of the capture group, ie "satya1" or "satya2"
}
See the live example more detail.
Use javascript function replace which helps you to use regex for this case
var string = '=---satya1';
string = string.replace(/[^a-zA-Z0-9]/g, '');

Categories