function extractSummary(iCalContent) {
var rx = /\nSUMMARY:(.*)\n/g;
var arr = rx.exec(iCalContent);
return arr[1];
}
You need these changes:
Put the
*inside the parenthesis as suggested above. Otherwise your matching group will contain only one character.Get rid of the
^and$. With the global option they match on start and end of the full string, rather than on start and end of lines. Match on explicit newlines instead.I suppose you want the matching group (what's inside the parenthesis) rather than the full array?
arr[0]is the full match ("\nSUMMARY:...") and the next indexes contain the group matches.String.match(regexp) is supposed to return an array with the matches. In my browser it doesn't (Safari on Mac returns only the full match, not the groups), but Regexp.exec(string) works.
function extractSummary(iCalContent) {
var rx = /\nSUMMARY:(.*)\n/g;
var arr = rx.exec(iCalContent);
return arr[1];
}
You need these changes:
Put the
*inside the parenthesis as suggested above. Otherwise your matching group will contain only one character.Get rid of the
^and$. With the global option they match on start and end of the full string, rather than on start and end of lines. Match on explicit newlines instead.I suppose you want the matching group (what's inside the parenthesis) rather than the full array?
arr[0]is the full match ("\nSUMMARY:...") and the next indexes contain the group matches.String.match(regexp) is supposed to return an array with the matches. In my browser it doesn't (Safari on Mac returns only the full match, not the groups), but Regexp.exec(string) works.
You need to use the m flag for ^ and $ to work as expected:
[...] if
mis used,^and$change from matching at only the start or end of the entire string to the start or end of any line within the string.
Also put the * in the right place:
let iCalContent = "DATE:20091201T220000\r\nSUMMARY:Dad's birthday";
let result = /^SUMMARY\:(.*)$/gm.exec(iCalContent);
console.log(result && result[1]);
regex - Extract text in Regular Expression in Javascript - Stack Overflow
javascript - How to extract a text using regex? - Stack Overflow
regex - Using Regular Expressions to Extract Values in Javascript - Stack Overflow
javascript - How to extract text using Regular expression (RegEx)? - Stack Overflow
match returns an array.
The default string representation of an array in JavaScript is the elements of the array separated by commas. In this case the desired result is in the second element of the array:
var tesst = "afskfsd33j"
var test = tesst.match(/a(.*)j/);
alert (test[1]);
Each group defined by parenthesis () is captured during processing and each captured group content is pushed into result array in the same order as groups within pattern starts. See more on http://www.regular-expressions.info/brackets.html and http://www.regular-expressions.info/refcapture.html (choose right language to see supported features)
var source = "afskfsd33j"
var result = source.match(/a(.*)j/);
result: ["afskfsd33j", "fskfsd33"]
The reason why you received this exact result is following:
First value in array is the first found string which confirms the entire pattern. So it should definitely start with "a" followed by any number of any characters and ends with first "j".
Second value in the array is captured group defined by parenthesis. In your case group contains string consist of any character with length <0; infinite). Which is entire source without starting "a" and ending "j" which are defined outside parenthesis, so exactly "fskfsd33".
If you want to get rid of second value in the array you may define pattern like this:
/a(?:.*)j/
where "?:" means that group of chars which match the content in parenthesis will not be part of the resulting array.
Other options might be in this simple case to write pattern without any group because it is not necessary to use group at all:
/a.*j/
If you want to just check whether source text matches the pattern and does not care about found texts then you may try:
var result = /a.*j/.test(source);
The result should return then only true|false values. For more info see http://www.javascriptkit.com/javatutors/re3.shtml
I would recommend test page https://regex101.com/ where you can test the patterns with full explanation how the RegExp works.
Here's a solution using a regex object and exec, which is safer than filtering out parenthesis with something like str.match(/\w+|\((.*?)\)/g).map(e => e.replace(/^\(|\)$/g, "")):
var str = "abc (24 314 83 383)(-256)sa 0 (24 314) 1";
var reg = /\w+|\((.*?)\)/g;
var match;
var res = [];
while (match = reg.exec(str)) {
res.push(match[1] || match[0]);
}
console.log(res);
You can try this:
let str = "abc (24 314 83 383)(-256)sa 0 (24 314) 1";
let replaced = str.replace(/(\s*\(|\))/g, '<REP>');
let arrFromStr = replaced.split('<REP>').filter(w => w.length != 0);
Variable "replaced" replaces all 1) 0 or more spaces + "(", and 2) all ")" symbols to "" string. arrFromStr creates an array from string and split it with "". Then we check is the element of array empty, or not.
JavaScript’s regular expressions don’t support look-behind assertions (i.e. (?<=…) and (?<!…)), so you can’t use that regular expression. But you can use this:
#@#(.+)(?=#-#)
Then just take the matched string of the first group. Additionally, to only match as little as possible, make the + quantifier non-greedy by using +?.
The group (.+) will match as much as it can (it's "greedy"). To make it find a minimal match you can use (.+?).
You can do something like this:
var str = "10M255S"
var match = str.match(/^([0-9]*)M*([0-9]*)S*$/)
Then match[1] is 10 and match[2] is 255
If var str = "10M", then match[1] is 10 and if var str = "255S", then match[1] is 255
In any of the three cases, matches start from second element of the array match.
Hope this helps.
you can use .split() to split the strings:
var str = "10M225S"
var m = str.split("M")[0];
var s = str.split("M")[1].split("S")[0];
console.log("m >>> " + m);
console.log("s >>> " + s);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
no need for regular expressions in this case.
You can use JavaScript Regular expressions' exec method.
var regex = /\#([^\$]+)\$/g,
match_arr,
result = [],
str = "some #extract1$ more #extract2$ text #extract3$ some junk";
while ((match_arr = regex.exec(str)) != null)
{
result.push(match_arr[1]);
}
console.log(result);
Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec
Regex for you will be like #(?<extract>[\s\S]*?)\$
Named group 'extract'will contain the values you want.
As Alex mentioned, if javascript doesnt support named group, we can use numbered group. So modified regex will be #([\s\S]*?)\$ and desired values will be in group number 1.
try this
var regex = /@\{([^;]+); ([^\}]+)(@.*)\}/
$1 => Name
$2=> 11112121
Here is the working example
Use match instead, like this:
var regex = /@\{([^;]+);\s+([^@]+)/;
var matches = text.match(regex);
alert(matches[1] + ', ' + matches[2]);
http://jsfiddle.net/rooseve/bM2U6/
const myString = "This is my comment content. [~firstName.lastName]";
const first = myString.match(/.*\~(.*)\./)[1]
const last = myString.match(/.*\.(.*)]/)[1]
const both = myString.match(/.*\~(.*)\]/)[1]
console.log(first)
console.log(last)
console.log(both)
Here's a good website for regex help
You can try this-
let myString = "This is my comment content. [~firstName.lastName] and the original name is [~John.Doe]";
const regex = /\[~([^\]]+)\.([^\]]+)\]/g;
let match = regex.exec(myString);
const names = [];
while(match !== null) {
names.push({firstName: match[1], lastName: match[2]});
match = regex.exec(myString);
}
// Remove the pattern from the original string.
myString = myString.replace(regex, '');
console.log(names, myString);
This code will find all the matches found for the pattern.
Hey!
So I am currently trying to extract a substring out of another string using Regex capture groups in JavaScript. However, it doesn't really work since my Regex matches the entire string only.
This is the string I am working with:
'"updateUsersRoom",{"room":"room_name","userid":111,"imgthumb":"some.png"}]'This is the part I wanna extract:
'{"room":"room_name","userid":111,"imgthumb":"some.png"}'Note
The following case is also possible:
'"someEvent", 1234]'
Here I'd want to extract
'1234'
The regular expression I am currently using is:
const eventDataRegex = new RegExp(`"${eventName}",(.*)]`, 'g');
-> resolves to /"updateUsersRoom",(.*)]/g for example
The result I am receiving using JavaScript's provided Str.match() function is the entire string, no groups whatsoever.
Example result:
[
'"updateUsersRoom",{"room":"room_name","userid":111,"imgthumb":"some.png"}]'
]Can anyone help me out on this one? I only recently stated using regex :)
Thank you all in advance!