Java Script Regex Is not working as expected [duplicate] - javascript

What is the regular expression (in JavaScript if it matters) to only match if the text is an exact match? That is, there should be no extra characters at other end of the string.
For example, if I'm trying to match for abc, then 1abc1, 1abc, and abc1 would not match.

Use the start and end delimiters: ^abc$

It depends. You could
string.match(/^abc$/)
But that would not match the following string: 'the first 3 letters of the alphabet are abc. not abc123'
I think you would want to use \b (word boundaries):
var str = 'the first 3 letters of the alphabet are abc. not abc123';
var pat = /\b(abc)\b/g;
console.log(str.match(pat));
Live example: http://jsfiddle.net/uu5VJ/
If the former solution works for you, I would advise against using it.
That means you may have something like the following:
var strs = ['abc', 'abc1', 'abc2']
for (var i = 0; i < strs.length; i++) {
if (strs[i] == 'abc') {
//do something
}
else {
//do something else
}
}
While you could use
if (str[i].match(/^abc$/g)) {
//do something
}
It would be considerably more resource-intensive. For me, a general rule of thumb is for a simple string comparison use a conditional expression, for a more dynamic pattern use a regular expression.
More on JavaScript regexes: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

"^" For the begining of the line "$" for the end of it. Eg.:
var re = /^abc$/;
Would match "abc" but not "1abc" or "abc1". You can learn more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Related

Matching patterns not within a set of opening and closing characters e.g {}, ()

I have this string pattern below
str = "nums#1#2#3{#4}#5"
Its there a way I can match all the #\d+ patterns excluding the ones within the curly braces.
I am currently achieving the desired result by replace the curly braces and everything withing them with an empty string before matching.
str = str.replace(/\{[^}]*\}/g, '');
match = str.match(/#\d+/g);
Its there a way to do this in javascript regular expression without the first replacement?
Assuming { and } are balanced, you can use this negative lookahead to match numbers not within {...}:
var str = "nums#1#2#3{#4}#5";
var arr = str.match(/#\d+(?![^{]*})/g)
console.log(arr)
//=> ["#1", "#2", "#3", "#5"]
(?![^{]*} is a negative lookahead that asserts after a number we don't have a } ahead before matching a {
The way is to capture all that you don't want before, example:
var result = txt.replace(/((?:{[^}]*}|[^#{]|#(?!\d))*)(#\d+)/g, '$1 number:$2 ');
Yes, use this one : (?!{)#\d(?!})
Demo
var str = "nums#1#2#3{#4}#5";
var result=str.match(/#\d+(?!})/g);
console.log(result);
you can write like this too.

Add condition to regex and convert it from js to vb.net

I have this regex expression(match either English or Hebrew chars, but not combined):
/^(?:[\u0590-\u05FF\uFB1D-\uFB40]+|[\w]+)$/i
It works ok, I just need to also add limitaion so no numbers would be allow.
This should match: abc, אבג
This should not be match: a1, 1b, aא,
The same limitation also need to be added to this regex expression:
/^(?:[\u0590-\u05FF\uFB1D-\uFB40 ]+|[\w ]+)$/i
Its purpose is the same as the first one, only that spaces are allowed.
This should match: abcx, abcx ascx, דגהק ,שגד דשגב
This should not be match: asaceדגעההת, ascasv אקיכרעקכ, as3, a3s, אב3ע
Also, if someone can help me to convert the new regex expression I requested,
and also this on:
/^05\d{8}$/i
from JavaScript to VB, I'd be most grateful.
Just use [A-Za-z] instead of \w if you don't want to allow numbers. If you are using the /i flag, you can also just use [a-z].
var regex = /^(?:[\u0590-\u05FF\uFB1D-\uFB40 ]+|[a-zA-Z ]+)$/i;
var texts = ["abcx", "abcx ascx", "דגהק" ,"שגד דשגב", "asaceדגעההת", "ascasv אקיכרעקכ", "as3", "a3s", "אב3ע"];
for(var i=0; i<texts.length; i++) {
var text = texts[i];
console.log(text + ": ", !!text.match(regex));
}

regex lookbehind in javascript

i im trying to match some words in text
working example (what i want) regex101:
regex = /(?<![a-z])word/g
text = word 1word !word aword
only the first three words will be matched which is what i want to achieve.
but the look behind will not work in javascript :(
so now im trying this regex101:
regex = /(\b|\B)word/g
text = word 1word !word aword
but all words will match and they may not be preceded with an other letter, only with an integer or special characters.
if i use only the smaller "\b" the 1word wont matchand if i only use the "\B" the !word will not match
Edit
The output should be ["word","word","word"]
and the 1 ! must not be included in the match also not in another group, this is because i want to use it with javascript .replace(regex,function(match){}) which should not loop over the 1 and !
The code i use it for
for(var i = 0; i < elements.length; i++){
text = elements[i].innerHTML;
textnew = text.replace(regexp,function(match){
matched = getCrosslink(match)[0];
return "<a href='"+matched.url+"'>"+match+"</a>";
});
elements[i].innerHTML = textnew;
}
Capturing the leading character
It's difficult to know exactly what you want without seeing more output examples, but what about looking for either starts with boundary or starts with a non-letter. Like this for example:
(\bword|[^a-zA-Z]word)
Output: ['word', '1word', '!word']
Here is a working example
Capturing only the "word"
If you only want the "word" part to be captured you can use the following and fetch the 2nd capture group:
(\b|[^a-zA-Z])(word)
Output: ['word', 'word', 'word']
Here is a working example
With replace()
You can use specific capture groups when defining the replace value, so this will work for you (where "new" is the word you want to use):
var regex = /(\b|[^a-zA-Z])(word)/g;
var text = "word 1word !word aword";
text = text.replace(regex, "$1" + "new");
output: "new 1new !new aword"
Here is a working example
If you are using a dedicated function in replace, try this:
textnew = text.replace(regexp,function (allMatch, match1, match2){
matched = getCrosslink(match2)[0];
return "<a href='"+matched.url+"'>"+match2+"</a>";
});
Here is a working example
You can use the following regex
([^a-zA-Z]|\b)(word)
Simply use replace like as
var str = "word 1word !word aword";
str.replace(/([^a-zA-Z]|\b)(word)/g,"$1"+"<a>$2</a>");
Regex

Regex to capture typed information in TypeScript

I'm new to Regex, took some tutorials, having a hard time getting this right.
I'm working on a language support for TypeScript for a text editor, and I need a regex that matches JUST the typing information in a function. For example:
function asdf(param1:type1, param2:type2, param3:type3) {...}
In that, my regex should match 'type1', 'type2', 'type3' etc.
Here's what I'm trying:
\(.+?:(\w).+?\)
Breaks down like this:
\( look for starting parenthesis
.+?: any number of characters up to the colon
(\w) capture group: the next word
.+? There may be additional words after this
\) Close parentheses for the end of the function parameters.
Not sure what I'm doing wrong, but like I said I'm new to regex. Currently it captures the entire stuff, from beginning parens to closing parens. I need it to match JUST the single words after the colon.
NOTE: I want to make sure it only matches words after colons inside parens, so that it doesn't match object key/data combos, which look similar in Javascript.
Thanks!
Try thus
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
function getParamNames(func) {
var fnStr = func.toString().replace(STRIP_COMMENTS, '')
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES)
if(result === null)
result = []
for(i=0; i< result.length; i++)
result[i]=result[i].split(":")[1];
return result
}
how to use
getParamNames("function asdf(param1:type1, param2:type2, param3:type3) {...}");
reference
How to get function parameter names/values dynamically from javascript

How do I make a regular expression that matches everything on a line after a given character?

If I have a String in JavaScript
key=value
How do I make a RegEx that matches key excluding =?
In other words:
var regex = //Regular Expression goes here
regex.exec("key=value")[0]//Should be "key"
How do I make a RegEx that matches value excluding =?
I am using this code to define a language for the Prism syntax highlighter so I do not control the JavaScript code doing the Regular Expression matching nor can I use split.
Well, you could do this:
/^[^=]*/ // anything not containing = at the start of a line
/[^=]*$/ // anything not containing = at the end of a line
It might be better to look into Prism's lookbehind property, and use something like this:
{
'pattern': /(=).*$/,
'lookbehind': true
}
According to the documentation this would cause the = character not to be part of the token this pattern matches.
use this regex (^.+?)=(.+?$)
group 1 contain key
group 2 contain value
but split is better solution
.*=(.*)
This will match anything after =
(.*)=.*
This will match anything before =
Look into greedy vs ungreedy quantifiers if you expect more than one = character.
Edit: as OP has clarified they're using javascript:
var str = "key=value";
var n=str.match(/(.*)=/i)[1]; // before =
var n=str.match(/=(.*)/i)[1]; // after =
var regex = /^[^=]*/;
regex.exec("key=value");

Categories