Reputation: 2093
i have regex code:
<script type="text/javascript">
var str = "kw-xr55und";
var patt1 = /[T|EE|EJU].*D/i;
document.write(str.match(patt1));
</script>
it can read:
str= "KD-R55UND" -> as UND
but if i type:
str= "kw-tc800h2und -> result tc-800h2und. //it makes script read T in front of 800
i want the result as UND
how to make the code just check at character behind the 800?
After this code it can work:
<script type="text/javascript">
var str = "kw-tc800h2und";
var patt1 = /[EJTUG|]\D*D/i;
document.write(str.match(patt1));
</script>
but show next problem, i can show the result if:
str= "kw-tc800un2d"
i want result like -> UN2D
Upvotes: 0
Views: 199
Reputation: 75222
Try this:
var patt1 = /(T|EE|EJU)\D*$/i;
It will match a sequence of non-digit characters starting with T
, EE
or EJU
, and finishing at the end of the string. If the string has to end with D
as in your examples, you can add that in:
var patt1 = /(T|EE|EJU)\D*D$/i;
If you want to match it anywhere, not just at the end of the string, try this:
var patt1 = /(T|EE|EJU)\D*D/i;
EDIT: Oops! No, of course that doesn't work. I tried to guess what you meant by [T|EE|EJU]
, because it's a character class that matches one of the characters E
, J
, T
, U
or |
(equivalent to [EJTU|]
), and I was sure that couldn't be what you meant. But what the heck, try this:
var patt1 = /[EJTU|]\D*D/i;
I still don't understand what you're trying to do, but sometimes trial and error is the only way to move ahead. At least I tested it this time! :P
EDIT: Okay, so the match can contain digits, it just can't start with one. Try this:
var patt1 = /[EJTU|]\w*D/i;
Upvotes: 1
Reputation: 3502
Try /[TEUJ]\D*\d*\D*D$/i
if it can have 1 digit in it, but not 2. Getting any more specific would require additional information, such as the maximum length of the string, or what exactly differs between parsing tc800h2und
and h2und
.
Upvotes: 0
Reputation: 526533
/(?<=\d)\D*/
It uses a lookbehind to find a set of non-digit characters that comes immediately after a digit.
/\D+$/
It will match any characters that are not digits from the end of the text backwards.
Upvotes: 1
Reputation: 12824
Depending on what flavor of regex you're using, you can use a look behind assertion. It will essentially say 'match this if it's right after a number'.
In python it's like this:
(?<=\d)\D*
Oh, also, regex is case sensitive unless you set it not to be.
Upvotes: 0