Hello can someone help me in jquery regex?
whew coz im stack here since last night and finally iv'e decided to ask some help :)
any here's my regex abd the string is in exg variable.. then i want to split the string each
matches[0] = 'eNortjI0sLBScgQDz3yTfK98XCdH59RKc4M8&+SSXFzzXFz3UE9H9yzfYMfCYtPiDLes0NSAXCL3nIj0osJcIvNCjwxLv6z8YhPTXFxv8&KSMNekjIrgqqzQvOJyy0zXNPMoZ4vS0PQS4+S0&IIgU7OssPIolygXJWtcMMMFXCch|eNortjI0sLRScgQDz3yTfK98XCfHXDBDjzx3X4&cXCLXygKn4tzsNCNcJ+NMk+xEM6Ok&OIq1+DcXFxLw8AwjyhHb480lyxTg&LkKv8sXw&zpCSnJE+XYo&EVH&3yKyAsMjEtKxSi4CIqlwigwL&giinXDC3wCiXKBcla1wwpPEmEA==|';
matches[1] = 'eNortjI0NLJScgQDz3yTfK98XCdH9yCPZJ&CiCpD36xcMMuSsLwox6qAwMqkUAPTlChHI8ugvDQL9zzjbBMfT8u8RIOgMgvnHJ9SpzynvFDfQAugijBLv6CgXDBT&0LzKMdI06BIf9OyKGd&U58kN19fV8colygXJWtcMNaqJP8=|';
var regex = /[a-zA-Z]+[0-9]+[/-=&_]+|/g;
var exg = 'eNortjI0sLBScgQDz3yTfK98XCdH59RKc4M8&+SSXFzzXFz3UE9H9yzfYMfCYtPiDLes0NSAXCL3nIj0osJcIvNCjwxLv6z8YhPTXFxv8&KSMNekjIrgqqzQvOJyy0zXNPMoZ4vS0PQS4+S0&IIgU7OssPIolygXJWtcMMMFXCch|eNortjI0sLRScgQDz3yTfK98XCfHXDBDjzx3X4&cXCLXygKn4tzsNCNcJ+NMk+xEM6Ok&OIq1+DcXFxLw8AwjyhHb480lyxTg&LkKv8sXw&zpCSnJE+XYo&EVH&3yKyAsMjEtKxSi4CIqlwigwL&giinXDC3wCiXKBcla1wwpPEmEA==|eNortjI0NLJScgQDz3yTfK98XCdH9yCPZJ&CiCpD36xcMMuSsLwox6qAwMqkUAPTlChHI8ugvDQL9zzjbB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0KJcoFyVrXDA8ZVwnTQ==|eNortjI0MrBScgQDz3yTfK98XCdHLy&LSLfMyOxcIvfIioJit9Lc9NC04NK8NEvncHe&9Byv5MRwy8pC79ywgLT0SJ+qjORCC5PK0oh8i8yU0gpPR9PK7BJXn9SCxDznsEQXb7e8LLMCp+L8tMTyKJcoF7XUioLMotTi+Mw8WwMla1ww8SosXw==|eNortjI0NLRScgQDz3yTfK98XCdHr0pcJ+PAdP9sc+NcIvcyY4PEoNywyuyKzOQsC8twDx9Hg7TUkHyv7BDjMtPg3LwcjyinqlwiNxcXXCeXKFwn7wggEWmck59YHJhZ5Z4ZmF4R5eSfaFnkWuzpVFRglpxcXB7lEuWiZA1cMO+XJv4=|eNortjI0MLdScgQDz3yTfK98XCdHr7LgEpekEmO&xIBgo5CMoOSSKu&QDKPK5OziojS&&Bwvk&BK4yqLCFOjTNNcXC9Tz8C0jLzSkuCMYI88v8KIwORQi&TMQLM8i&B8Sy9Pl7QqXwt&gxQvrzLHKJcoFyVrXDCNviXB|eNortjI0NLRScgQDz3yTfK98XCdHn4pCp5yQxNIkM8MQ97SI0PLkoIqy9DJvZ3PXgPDwgKSIKKfwPKfi&CqvtAqTTNf0YMdwS8cM&yjH7Mr0omTTsLQqg&RcXLOK9Oz00LDgwijHCOeqyuC8KFwnf2fvSMcolygXJWtcMD&GXCfp|eNortjI0NLBScgQDz3yTfK98XCdHF6&gcEdTr5SiNO8KY1wnk1KLIE&&yowox+JcMB&XlGyzQH8&gwKvyIjyEMuyNI+ktMSQ7DDzdAsTA&fyKMckl&KS9HDDgNRKL0+jkCintLCStFwin+LcRC+PXFxcJ7+qwCiXKBcla1wwqjUmMg=='
if(regex.test(exg)) {
var matches = exg.match(regex);
for(var match in matches) {
alert(matches[match]);
}
} else {
alert("No matches found!");
}`
but my regex won't work whew can someone give me a right regex for it? :) please help..
Elias answer is probably the easiest way to do this but if you insist on regex then how about this:
var regex = /[a-zA-Z0-9\/-=&_+]+\|{0,1}/g
Explanation of your regex and why it doesn't work:
[a-zA-Z]+ // Match one or more a-z upper or lower case
[0-9]+ // *THEN* match one or more 0-9
[/-=&_]+ // *THEN* match one or more of these characters
| // *THEN* match a pipe
The problem here is that the letters, numbers and symbols in your search string are mixed together. Therefore they all need to go inside square brackets together so you match one or more of all of them together in any order. Yours puts them in a specific order, letters first, then numbers, then symbols.
The {0,1} on the end matches either zero or one pipe and will therefore catch the last match which does not have a pipe at the end.
Incidentely there's no such thing as JQuery regex. The regex functions are javascript.
erm... how about just using split like so marches = yourString.split('|');
this will return an array of strings, but the pipe char's will not be included, but just concat them to the substring if you need them.
You've missed a slash before |, so this may be what you want?
var regex = /[a-zA-Z0-9\/-=&_]+\|/g;
Related
I have an input string like this:
ABCDEFG[HIJKLMN]OPQRSTUVWXYZ
How can I replace each character in the string between the [] with an X (resulting in the same number of Xs as there were characters)?
For example, with the input above, I would like an output of:
ABCDEFG[XXXXXXX]OPQRSTUVWXYZ
I am using JavaScript's RegEx for this and would prefer if answers could be an implementation that does this using JavaScript's RegEx Replace function.
I am new to RegEx so please explain what you do and (if possible) link articles to where I can get further help.
Using replace() and passing the match to a function as parameter, and then Array(m.length).join("X") to generate the X's needed:
var str = "ABCDEFG[HIJKLMN]OPQRSTUVWXYZ"
str = str.replace(/\[[A-Z]*\]/g,(m)=>"["+Array(m.length-1).join("X")+"]")
console.log(str);
We could use also .* instead of [A-Z] in the regex to match any character.
About regular expressions there are thousands of resources, specifically in JavaScript, you could see Regular Expressions MDN but the best way to learn, in my opinion, is practicing, I find regex101 useful.
const str="ABCDEFG[HIJKLMN]OPQRSTUVWXYZ";
const run=str=>str.replace(/\[.*]/,(a,b,c)=>c=a.replace(/[^\[\]]/g,x=>x="X"));
console.log(run(str));
The first pattern /\[.*]/ is to select letters inside bracket [] and the second pattern /[^\[\]]/ is to replace the letters to "X"
We can observe that every individual letter you wish to match is followed by a series of zero or more non-'[' characters, until a ']' is found. This is quite simple to express in JavaScript-friendly regex:
/[A-Z](?=[^\[]*\])/g
regex101 example
(?= ) is a "positive lookahead assertion"; it peeks ahead of the current matching point, without consuming characters, to verify its contents are matched. In this case, "[^[]*]" matches exactly what I described above.
Now you can substitute each [A-Z] matched with a single 'X'.
You can use the following solution to replace a string between two square brackets:
const rxp = /\[.*?\]/g;
"ABCDEFG[HIJKLMN]OPQRSTUVWXYZ".replace(rxp, (x) => {
return x.replace(rxp, "X".repeat(x.length)-2);
});
I am trying to find a way to extract the numbers that occur after abc/ immediately succeeding the / and before any further letters, numbers or punctuation.
E.g:
abc/134567/something should return 1234567
abc/1234567?foo=bar should still only return 1234567
blah/1234/abc/678 should only return 678 as I'm looking only for the number that succeeds abc/
I'm aware there are two options: regex or substring match.
In order to perform the substring match I need the index point but I'm dubious about merely doing an indexOf("abc/") as it only returns the index of the first letter - a - which could be present elsewhere in the string.
With regex I have struggled as I find that searching for a mixture of the letters and the slashes seems to cause it to return null.
So what's the best way?
You can use this regexpression :
var rgx = new RegExp("abc\/([0-9]+)","gi");
Then :
var m = rgx.exec("abc/1234567?foo=bar");
console.log(m[0]);
edited after comments
You could use a regular expression and seach for abc/ and following digits.
var array = ['abc/134567/something', 'abc/1234567?foo=bar', 'blah/1234/abc/678'];
console.log(array.map(s => s.match(/abc\/(\d+)/)[1]));
We accept string that has abc/, after it an integer number, that is taken as a matched group and either the end of string or some non-digit symbol after it.
abc\/(\d+)(?:$|\D)
test
You'll use in Javascript for matched group extraction:
var myRegexp = /abc\/(\d+)(?:$|\D)/g;
var match = myRegexp.exec(inputString);
var result=match[1]; // the number after abc/
In another regex engine than that of JavaScript, lookahead and lookbehind could be used. But in JS lookbehinds are forbidden. :-(. So we have to use this, a bit more complicated, way.
Are you after something like this:
^(.*\/)(\d+)(.*)
Where the second group will give you the digits after the slash.
Look at the regex here
I'm looking for a regex (that I can implement in Javascript, so no lookbehinds) which will match all occurrences of a character, as long as it doesn't appear between two other characters. For example, I want to match all hyphens as long as they are not between plus signs.
----- // should match.
+---+ // should not match
---+---+--- // should only match the first 3 and last 3 characters.
I've tried adapting the method used in this post like so:
[-]+(?![^+]*\+)+
But it is not matching as desired. Any help is greatly appreciated.
P.S. Looking specifically for a REGEX solution. I realize this may not be the optimal solution but I'm specifically trying to improve my knowledge of regex.
If the delimiters (+ in your case) always come in pairs, you could use this:
var str = 'a-b-+-c-+-d-e'; // delimter is +
matches = str.match(/-(?=([^+]*\+[^+]*\+)*[^+]*$)/g, '');
console.log(matches); // 4 matches of `-`
This matches every hyphen that has an even number of pluses following it (could be zero).
Please try the following:
(?:^-+|-+$)
Regex101 demo: https://regex101.com/r/524iej/1
You can use use .replace to remove all text between 2 +s:
str = str.replace(/\+-*\+/g, '');
Example:
var str = '---+---+---';
str = str.replace(/\+-*\+/g, '');
//=> ------
I have string with 2 or 3 words:
'apple grape lemon'
'apple grape'
I need to get first char from all words.
my regex:
/^(\w).*?\ (\w).*?\ ?(\w?).*?$/
For all strings this regex get only first char of 2 words.
How to fix?
You cannot do this with one regex (unless you are using .NET). But you can use a regex that matches one first character of a word, then get all the matches, and join them together:
var firstLetters = '';
var match = str.match(/\b\w/g)
if (match)
firstLetters = match.join('');
Of course if you just want to get the letters on their own, there is no need for the join, since the match will simply be an array containing all those letters.
You should not, that \w is not only letters, but digits and underscores, too.
If you work with javascript, you don't need to regex the hell out of a simple problem.
To get the first letter, just do that:
var aString = 'apple bee plant';
var anArray = aString.split(' ');
for(var aWord in anArray) {
var firstLetter = aWord.charAt(0);
}
Regular expressions are a regular language, such that you cannot have this kind of repetition in them. What you want is to cut the string into individual tokes (which can be done via regular expressions to match the separator) and then apply an regular expression on each token. To get the first char from each word it is faster to use a substring operation instead of a regular expression.
The problem with your regex is that the .*? after the second word eats up all the following content as everything afterwards is optional. This could be solved, but I personally think it makes things more complicated than required.
The most simple way would be:
firstLetters = (m = str.match(/\b\w/g))? m.join('') : '';
In regexp "words" don't mean only letters. In JavaScript \w is equals [A-Za-z0-9_]. So if you want only letters in your result, you can use [A-Za-z].
I am having a difficult time getting a seemingly simple Regexp. I am trying to grab the last occurrences of word characters between square brackets in a string. My code:
pattern = /\[(\w+)\]/g;
var text = "item[gemstones_attributes][0][shape]";
if (pattern.test(text)) {
alert(RegExp.lastMatch);
}
The above code is outputting "gemstones_attributes", when I want it to output "shape". Why is this regexp not working, or is there something wrong with my approach to getting the last match? I'm sure that I am making an obvious mistake - regular expressions have never been my string suit.
Edit:
There are cases in which the string will not terminate with a right-bracket.
You can greedily match as much as possible before your pattern which will result in your group matching only the last match:
pattern = /.*\[(\w+)\]/g;
var text = "item[gemstones_attributes][0][shape]";
var match = pattern.exec(text);
if (match != null) alert(match[1]);
RegExp.lastMatch gives the match of the last regular expression. It isn't the last match in the text.
Regular expressions parse left to right and are greedy. So your regexp matches the first '[' it sees and grabs the words between it. When you call lastMatch it gives you the last pattern matched. What you need is to match everything you can first .* and then your pattern.
i think your problem is in your regex not in your src line .lastMatch.
Your regex returns just the first match of your square brackets and not all matches. You can try to add some groups to your regular expression - and normally you should get all matches.
krikit
Use match() instead of test()
if (text.match(pattern))
test() checks for a match inside a string. This is successfull after the first occurence, so there is no need for further parsing.