Javascript match one regex, but not another - javascript

How can I find all words in string which
match one expression:
/[a-zA-Z]{4,}/
but do not match another one:
/\b[a-zA-Z]([a-zA-Z])\1+[a-zA-Z]\b/
Something like pseudocode:
string.match( (first_expression) && ( ! second_expression) )

You could just do this:
string.match(/[a-zA-Z]{4,}/) && !string.match(/\b[a-zA-Z]([a-zA-Z])\1+[a-zA-Z]\b/)
But if you'd like to combine the patterns, you can use a negative lookahead ((?!...)), like this:
string.match(/^(?!.*\b[a-zA-Z]([a-zA-Z])\1+[a-zA-Z]\b).*[a-zA-Z]{4,}.*$/)
But this will reject the whole string if it finds the second pattern—e.g."fooz barz" will return null.
To ensure the words you find do not match the other pattern, try this:
string.match(/\b(?![a-zA-Z]([a-zA-Z])\1+[a-zA-Z]\b)[a-zA-Z]{4,}\b/)
In this case, "fooz barz" will return "barz".
Note that this can be cleaned up a bit by using the case insensitive flag (i):
string.match(/\b(?![a-z]([a-z])\1+[a-z]\b)[a-z]{4,}\b/i)

if(string.match(first_expression))
{
if(!string.match(second_expression))
{
//Do something important
}
}
This should match what you want and not what you don't.

Related

How would I make a regular expression that would mean /some string/ either followed by a line break or not?

function(input){
return input.replace(/teststring/ig, "adifferentstring");
}
I want to replace "teststring" and "teststring\n" with "adifferentstring"
In regex, to match a specific character you can place it in brackets:
[\n]
To make the match "optional", you can follow it with a ?:
[\n]?
In your exact example, your full regex could be:
teststring[\n]?
So, your function would look like:
function replace(input) {
return input.replace(/teststring[\n]?/ig, "adifferentstring");
}
I'd suggest going with matching characters in brackets as this makes for easy expansion; consider, for instance, that you want to match Window's newlines (a carriage-return + a newline):
teststring[\r\n]?
Try
function(input){
return input.replace(/teststring\n?/ig, "adifferentstring");
}
Try .replace(/teststring[\n]?/ig,"adifferentstring");
It would be something like this:
var re = /teststring([\n]?)/ig;
So then your replace statement would look about like this:
return input.replace(re,"adifferentstring");
Here's a fiddle showing the regex works.
And then a fiddle showing the replace operation working.
Edit:
Actually, thinking about the problem a little further, if your regex does match a carriage return or new line character, that would need to get put back into the replacing string. The same regex I posted originally will work but you will need this replace statement instead (with the $1 denoting the first group in parantheses.
return input.replace(re,"adifferentstring$1");
fiddle

Using variable in a regex

say i have the following variables:
myId; //Email_PDF
currentHoverOnItem; //Email_PDF_english of Email_PDF_Dutch or ...
So the first value is "Email_PDF" and the second is "Email_PDF_english". What i want i when currentHoverOnItem contains myId, then something can be executed.
This is what i have so far:
var pattern = new RegExp("^/"+myId+"/$");
if (currentHoverOnItem.match(pattern))
{
//Do something
}
Is this the right way to use the regex? It should match part of the string, there can be text before or after the match.
Is this the right way to use the regex?
No! Regexes are for patterns, not for literal strings. Use indexOf
if (currentHoverOnItem.indexOf(myId) >= 0)
{
//Do something
}
Try this
var pattern = new RegExp(myId, "g");
if (currentHoverOnItem.match(pattern))
{
//Do something
}
Your pattern is "anchored", meaning that ^ and $ specifiy begin and end of your string.
To match "Email_PDF_english" in an anchored pattern you could use
^Email_PDF_(.*)$, but it won't match if your string is longer, as your comment suggests.
If it's not anchored you could test for a blank following the Email_PDF_... string, ie
Email_PDF_([^\s]*)\s+
You need not use a reg_exp here. Using indexOf will do the trick for you
if(currentHoverOnItem.indexOf(myId) != -1)
{
//Do something
}

Javascript wildcard variable?

The value of product_id might be some combination of letters and numbers, like: GB47NTQQ.
I want to check to see if all but the 3rd and 4th characters are the same.
Something like:
if product_id = GBxxNTQQ //where x could be any number or letter.
//do things
else
//do other things
How can I accomplish this with JavaScript?
Use regular expression and string.match(). Periods are single wildcard characters.
string.match(/GB..NTQQ/);
Use a regular expression match:
if ('GB47NTQQ'.match(/^GB..NTQQ$/)) {
// yes, matches
}
Answers so far have suggested match, but test is likely more appropriate as it returns true or false, whereas match returns null or an array of matches so requires (implicit) type conversion of the result within the condition.
if (/GB..NTQQ/.test(product_id)) {
...
}
if (myString.match(/regex/)) { /*Success!*/ }
You can find more information here: http://www.regular-expressions.info/javascript.html

How match a regex if it does not contain a particular word?

I want to write a regex in Python or JavaScript to match if the given string does not JUST contain the given word (e.g "any").
For example :
any : does not match
AnY : does not match
anyday : match
any day : match
blabla : match
If you also need words other that starting with "any" you can use a negative lookahead
^(?!any$).*$
This will match anything besides "any".
It is probably more efficient to not use a regex, this works as well:
def noAny(i):
i = i.lower().replace('any', '')
return len(i) > 0
Something like this:
/(any)(.+)/i
any.+
..and some text to make the 30char threshold
Use string.match(regexp) method in javascript for this purpose. See the code below:
<script type="text/javascript">
var str="source string contains YourWord";
var patt1=/YourWord/gi; // replace YourWord within this regex with the word you want to check.
if(str.match(patt1))
{
//This means there is "YourWord" in the source string str. Do the required logic accordingly.
}
else
{
// no match
}
</script>
Hope this helps...

JavaScript: string.match regular expression help

I have a javascript function that looks element id with certain patterns. So I have the following script:
if (f.elements[i].id.match(/DataList\[-\d{3}|\d{3}\]\.MemberId/)) {
//do something
}
It should match elements with ids such as these:
DataList[-1].MemberId
DataList[-2].MemberId
And it does, however it also matches the following:
DataList[-1].FirstName
DataList[-2].FirstName
which I don't want.
Could any guru take a look at the regular expression above and point out what is going wrong?
Thanks,
Cullen
Try to anchor your regex at the beginning with a ^ and at the end with a $, group your digit match and allow 1-3 digits instead of just 3.
if (f.elements[i].id.match(/^DataList\[(-\d{1,3}|\d{1,3})\]\.MemberId$/)) {
//do something
}
The way you had it, it was matching anything containing "DataList[-123" or containing "123].MemberId".
A simpler overall regex that accomplishes the same thing is:
if (f.elements[i].id.match(/^DataList\[-?\d{1,3}\]\.MemberId$/)) {
//do something
}
The or is saying:
DataList\[-\d{3} OR \d{3}\]\.MemberId/
This regex matches correctly:
DataList\[-?\d{1,3}\]\.MemberId
My suggestion
if (f.elements[i].id.match(/DataList\[-[0-9]{1,3}\]\.MemberId/)) {
}
The {} determines how many #s you want to support so 1-3 would match upu to [999]

Categories