im trying to regex this, but it doesnt work:
this is my string:
asdasd2-bgbegebr23-yiyity23-iopip123
So im trying to get: all values between '-', but it doesnt work: im using actually this:
/(-)(.*)(-)/gi
as regex, but doesnt work, thanks everyone :S
That's because the dot includes de dash. You should remove the dash. Try this:
/([^-]+)/gi
I don't understand very well the purpose of your regex. Supposing that you want to desgin a regular expression that iterated over the example string gets succesively asdasd2, bgbegebr23, etc, the regular expression will be something like this:
\-?([^\-]*)\-?
Why??
- : You have to use "-" instead of "-" because the hyphen is a special char for regular expressions, so you have to escape it
? : The hyphen is optional: in the first case (asdasd2), it is not present. So, enforcing it will omit this first case.
() : Grouping to catch all the asdsas2 and that alphanumeric stuff..
[^-] : Everything except hyphen. The approximation of #vks (.*?) I think will work also
- : Again, a hyphen, but escaped. And we cannot forget the "?" at the end because the last case of the example string, which doesn't end in hyphen.
And don't forget that if you are working in javascript you might need to scape the backslash (), resulting in an expression like this:
\\-?[^\\-]*)\\-?
Related
I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?
I am trying to match a complex if else regex pattern using jquery.validation
//My regex
(?=^abc1)(^abc1[-]dba\d*.csv)|(?=^def2)(^def2[-]dba\d*.xls|txt)|(?=^ghi3)(^ghi3[-]dba\d*.xls)|(?=^xyz4)(^xyz4[-]dba\d*.csv)
// Example
abc1-dba.csv
Tried over here : http://www.regexr.com/ it matches
But in the code it is not matching anything.
//I call the validate function inside which :-
rules : {
param : {
required : true,
pattern: "(?=^abc1)(^abc1[-]dba\d*.csv)|(?=^def2)(^def2[-]dba\d*.xls|txt)|(?=^ghi3)(^ghi3[-]dba\d*.xls)|(?=^xyz4)(^xyz4[-]dba\d*.csv)"
}
}
doesn't work.
There are some errors in your regex, but the reason it's failing on abc1-dba10.csv is because you're writing the regex as a string literal and you forgot to escape the backslashes. Your \d* should be \\d*.
As for the errors, the most significant one is where you test for .xls or .txt extensions. You need to isolate that part in its own group, like this: \.(?:xls|txt). You also forgot to escape the period, not just in that spot but everywhere. (You would probably never get bitten by that one, but it is a bug.)
Aside from that, you've got way more start anchors (^) than you need, but you left out the end anchor ($) entirely. And those lookaheads aren't doing anything useful. If the string matches the regex ^abc1[-]dba\d*.csv, then of course a lookahead like (?=^abc) at the beginning of the string will succeed. Here's how I would write it:
"^(?:abc1-dba\\d*\\.csv|def2-dba\\d*\\.(?:xls|txt)|ghi3-dba\\d*\\.xls|xyz4-dba\\d*\\.csv)$
If you can use a regex literal instead, use this:
/^(?:abc1-dba\d*\.csv|def2-dba\d*\.(?:xls|txt)|ghi3-dba\d*\.xls|xyz4-dba\d*\.csv)$/
I have string look like this :
"fdsgsgf.signature=xxxxx(bv)"
And i want to get xxxxx
With : var testRE = html.match(".signature=(.*)/\(");
And when i run it i get exception that it's not valid regex.
Any idea why?
Some issues with your code:
You're missing starting slash / of your regex
Instead of .* you should better use [^(]+
dot needs to be escaped
Modified code:
html.match(/\.signature=([^(]+)/);
You need to double escape the backslash: ".signature=(.*)/\\(". This is a valid regex, but it will match the / char though. If you don't need it, simply remove it ;)
I am a Regex newbie and trying to implement Regex to replace a matching pattern in a string only when it has a ( - open parentheses using Javascript. for example if I have a string
IN(INTERM_LEVEL_IN + (int)X_ID)
I would only like to highlight the first IN( in the string. Not the INTERM_LEVEL_IN (2 ins here) and the int.
What is the Regex to accomplish this?
To match the opening bracket you just need to escape it: IN\(.
For instance, running this in Firebug console:
enter code here"IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN()/, 'test');`
Will result in:
>>> "IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN\()/, 'test');
"testINTERM_LEVEL_IN + (int)X_ID)"
Parenthesis in regular expressions have a special meaning (sub-capture groups), so when you want them to be interpreted literally you have to escape them by with a \ before them. The regular expression IN\( would match the string IN(.
The following should only match IN( at the beginning of a line:
/^IN\(/
The following would match IN( that is not preceded by any alphanumeric character or underscore:
/[a-zA-Z0-9_]IN\(/
And finally, the following would match any instance of IN( no matter what precedes it:
/IN\(/
So, take your pick. If you're interested in learning more about regex, here's a good tutorial: http://www.regular-expressions.info/tutorial.html
You can use just regular old Javascript for regex, a simple IN\( would work for the example you gave (see here), but I suspect your situation is more complicated than that. In which case, you need to define exactly what you are trying to match and what you don't want to match.
I have a string like this: ----------- 243f,33f----
Now I want to remove all the - chars except the first -, the , and the numbers. The result should be -243,33. How would I do this?
Your question still isn't very clear, but this yields the output you want:
'----------- 243f,33f----'.replace(/(?!^-)[^\d,]+/g, '')
The regex matches one or more of any character except digits or commas, after making sure the first character isn't a hyphen at the beginning of the string.
EDIT: To those who came up with regexes using (?<!^) and then withdrew them because JavaScript doesn't support lookbehinds, that wasn't necessary. I know there were other problems with those answers, but for the purpose of matching "not the beginning of the string", (?!^) works just fine.
'----------- 243f,33f----'.replace(/[^-0123456789,]/g, '').replace(/^-/, 'x').replace(/-/g, '').replace(/x/, '-')
output: '-243,33'
replace the substring from second character ala
string.charAt(0) + string.substring(1).replace("-","");
but i don't know if the syntax is correct
EDIT:
oh if you want to remove the f's too you can remove any nondigit and non-comma:
string.charAt(0) + string.substring(1).replace("[^0-9,]","");
Try this one:
(?:(^-)|[^\d,\r\n])
and replace with
$1
See it here on Regexr
Its a bit more difficult since Javascript does not support look behinds. So if there is a - at the start of the line it will be replaced by itself, for the other matches $1 is empty so replaced with nothing.