Extract alphanumeric words starting with letter, ignore others - javascript

Sounds easy enough, but I am just unable to figure that out.
/([a-z][a-z0-9]+)/gi
Test case looks like this
Correct #123 2baZ #1a2 thisToo $bar andTwo2
I am fighting with those partial matches. Only valid should be: ["Correct", "thisToo", "andTwo2"]. Any others should stay unmatched.
Here is link to tester: http://regex101.com/r/qG7lU9/8
Update:
Here is JS fiddle that works better than tester itself... http://jsfiddle.net/FredyCr/6hsgef82/

You can use lookahead and non-capturing group based regex like this:
(?:^| )([a-z][a-z0-9]+(?= |$))
And use captured group #1 for your matches that gives:
Correct
thisToo
andTwo2
RegEx Demo
Code:
var rx = /(?:^| )([a-z][a-z0-9]+(?= |$))/gi
var str = " Correct #123 2baZ #1a2 thisToo $bar andTwo2";
var matches = [];
while (match = rx.exec(str))
matches.push(match[1]);
console.log(matches);
//=> ["Correct", "thisToo", "andTwo2"]
JsFiddle Demo

Get the matched strings from group index 2.
(?:^| )([a-z][a-z0-9]+)(?: |$)
DEMO
Javascript code would be,
> var re = /(?:^| )([a-z][a-z0-9]+)(?: |$)/gi
undefined
> var str = " Correct #123 2baZ #1a2 thisToo $bar andTwo2";
undefined
> var matches = [];
undefined
> while (match = re.exec(str))
... {
... matches.push(match[1]);
... }
3
> console.log(matches);
[ 'Correct', 'thisToo', 'andTwo2' ]

var regex = /(^|\s)[a-z][a-z0-9]+/gi;
var text = "Correct #123 2baZ #1a2 thisToo $bar andTwo2";
var found;
while ((found = regex.exec(text)) !== null)
console.log(found[0].trim());
Output
Correct
thisToo
andTwo2

Workaround: split and filter the tokens you want:
var rx = /^[a-zA-Z][a-zA-Z0-9]*$/;
var match = str.split(/\s+/).filter(rx.exec.bind(rx));
Example

Related

Regex match whole expression instead of 2 matches [duplicate]

I have the text:
s.events="event3"
s.pageName="Forum: Index"
s.channel="forum"
s.prop1="Forum: Index"
s.prop2="Index Page"
s.prop36=""
s.prop37=""
s.prop38=""
s.prop39=""
s.prop40="53"
s.prop41="Anonymous"
s.prop42="username"
s.prop43=""
s.prop47=""
s.eVar1="Forum: Index"
s.eVar2="Index Page"
s.eVar36=""
s.eVar37=""
saved in a var in javascript and I want to extract the text between the quotes of s.prop42 giving me the result:
"username"
what I have right now is
var regex = /\?prop.42="([^']+)"/;
var test = data.match(regex);
but it doesnt seem to work, can someone help me out?
Use this:
var myregex = /s\.prop42="([^"]*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[1];
}
In the regex demo, look at the capture group in the right pane.
Explanation
s\.prop42=" matches s.prop42=" (but we won't retrieve it)
The parentheses in ([^"]*) capture any chars that are not a " to Group 1: this is what we want
The code gets the Group 1 capture
Can't comment on above answer, but I think the regex is better with .* like so:
var myregex = /s\.prop42="(.*)"/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[1];
}

Find every instance of #<username> in paragraph in Javascript

I have a paragraph that could have 1 or more instances of "#" followed by different usernames. How can I find each instance of this in Javascript?
Something like this but it doesn't work yet:
var regexp = '/#([a-z0-9_]+)/i';
var post = "Hi #tom, where is #john and #nick?" ;
var match, matches = [];
while ((match = regexp.exec(post)) != null) {
matches.push(match.index);
}
console.log(matches);
Console log would read: #tom #john #nick
You have two mistakes in your code causing it not to work as you expect.
(1.) You need to remove the quotes from your regular expression and use the g (global) modifier. You can replace your character class to the shorter version \w and remove the case-insensitive modifier here.
var regexp = /#\w+/g
(2.) You need to reference the match instead of referencing the match.index
matches.push(match[0]);
Final solution:
var post = "Hi #tom, where is #john and #nick?";
var regexp = /#\w+/g
var match, matches = [];
while ((match = regexp.exec(post)) != null) {
matches.push(match[0]);
}
console.log(matches); //=> [ '#tom', '#john', '#nick' ]
Alternatively you can use the String.match method.
var post = 'Hi #tom, where is #john and #nick?',
result = post.match(/#\w+/g);
console.log(result); //=> [ '#tom', '#john', '#nick' ]

How to "place-hold" regex matches in array using javascript

If I have a multiple match regex like:
var matches = string.match(/\s*(match1)?\s*(match2)?(match3)?\s*/i);
and if my string that I am testing is like this:
var string = "match1 match3";
is there a way to output this array values:
matches[1] = "match1";
matches[2] = "";
matches[3] = "match3";
Notice: what I would like is for the regex to match the entire thing but "place-hold" in the array the matches it does not find.
Hope this makes sense. Thanks for the help!
There already is a "placeholder". Unmatched groups pop an array index matching the group number with an undefined value. e.g.
var someString = "match2";
var matches = someString.match(/\s*(match1)?\s*(match2)?(match3)?\s*/i);
matches now has
["match2", undefined, "match2", undefined]
Element 0 is the complete regex match and elements 1-3 are the individual groups
So you can do for example...
// check if group1
if (typeof matches[1] != 'undefined')
When you want to compare the string to the regexes, just do an Array join.Something like,
matches[1] = "match1";
matches[2] = "";
matches[3] = "match3";
var mystring = mathches.join("");
The joining character could be anything. You could also do,
var mystring = mathches.join(" ");
EDIT:
Not sure from the problem description, but i think you want the regex to output an array.Something like
text = "First line\nSecond line";
var regex = /(\S+) line\n?/y;
would give,
var match = regex.exec(text);
print(match[1]); // prints "First"
print(regex.lastIndex); // prints 11
More about it here

Using Regex to pull out a part of a string

I can't figure out how to pull out multiple matches from the following example:
This code:
/prefix-(\w+)/g.exec('prefix-firstname prefix-lastname');
returns:
["prefix-firstname", "firstname"]
How do I get it to return:
[
["prefix-firstname", "firstname"],
["prefix-lastname", "lastname"]
]
Or
["prefix-firstname", "firstname", "prefix-lastname", "lastname"]
This will do what you want:
var str="prefix-firstname prefix-lastname";
var out =[];
str.replace(/prefix-(\w+)/g,function(match, Group) {
var row = [match, Group]
out.push(row);
});
Probably a mis-use of .replace, but I don't think you can pass a function to .match...
_Pez
Using a loop:
re = /prefix-(\w+)/g;
str = 'prefix-firstname prefix-lastname';
match = re.exec(str);
while (match != null) {
match = re.exec(str);
}
You get each match one at a time.
Using match:
Here, the regex will have to be a bit different, because you cannot get sub-captures (or I don't know how to do it with multiple matches)...
re = /[^\s-]+(?=\s|$)/g;
str = 'prefix-firstname prefix-lastname';
match = str.match(re);
alert(match);
[^\s-]+ matches all characters except spaces and dashes/hyphens only if they are followed by a space or are at the end of the string, which is a confition imposed by (?=\s|$).
You can find the groups in two steps:
"prefix-firstname prefix-lastname".match(/prefix-\w+/g)
.map(function(s) { return s.match(/prefix-(\w+)/) })

javascript REGex remove single quote in match

var RegTxt = "$f1$='test' AND f2='test2'";
alert(RegTxt.match(/\'[^\']*'/g))
returns the match correctely i:e 'test','test2' but how can i remove the single quote in the match.
This would be quite simple if JavaScript supported negative lookbehinds:
/(?<=').*?(?=')/
But unfortunately, it doesn't.
In cases like these I like to abuse String.prototype.replace:
// btw, RegTxt should start with a lowercase 'r', as per convention
var match = [];
regTxt.replace(/'([^']*)'/g, function($0, $1){
match.push($1);
});
match; // => ['test', 'test2']
Here is a crude solution to your problem.
var match = RegTxt.match(/\'[^\']*'/g)
match = match.substring(1, match.length - 2);
Trivial approach:
RegTxt.replace(/'/g, "")
using your regex:
RegTxt.replace(/\'([^\']*)'/g, "$1")
var matches = str.match(regex);
var newMatches = [];
for( i in matches )
{
var word = matches[i];
newMatches.push( word.substring(1,word.length-1))
}
newMatches will now contain the array you need.

Categories

Resources