I want to get value of string's like follow.
If string's word start with some character like ("This is world").match(/^wo/g); It should return false But, i want it's true.
Because string has one word which start from wo.I don't want to execute line or string start with wo.
Please any one know using Regex how to check whole line's words check start with XYZ.
NOTE : I already Know that Line splitting with white-space and than after check every word start with XYZ But I don't want to split any line.
Thank you
firstly .match does NOT return boolean
you want regexp.test
(/\bwo/g).test("This is world");
\b matches a zero-width word boundary - see docs
try this :
var str = "The rain in SPAIN stays mainly in the plain";
var res = str.match(/ain/g);
A capturing or non-capturing group will do this job.
alert(/(^|\s)wo/.test("This is world"));
alert(/(^|\s)wo/.test("This is bar;world"));
Related
I'd like to match every word that starts with an exclamation point.
I got so far and it works. regex: ^!.
But now I wanna exclude the word "!test". So !hi matches but !test doesn't.
There's no NOT operator, so any ideas?
you can use the regex
function match(str){
let regex = /^!(?!test)\w+/;
return str.match(regex) != null;
}
console.log(match('!testing'))
console.log(match('!hihello'))
(?!test) is a negative look ahead which ensures !test is not matched
I am trying to find a pattern in a string that has a value that starts with ${ and ends with }. There will be a word between the curly brackets, but I won't know what word it is.
This is what I have \$\\{[a-zA-Z]\\}
${a} works, but ${aa} doesn't. It seems it's only looking for a single character.
I am unsure what I am doing wrong, or how to fix it and would appreciate any help anyone can provide.
I think this could help you
var str = "The quick brown ${fox} jumps over the lazy ${dog}";
var re = /\$\{([a-z]+)\}/gi;
var match;
while (match = re.exec(str)) {
console.log(match[1]);
}
Click Run code snippet and check your developer console for output
"fox"
"dog"
Explanation
+ means match 1 or more of the previous term — in this example, match 1 or more of [a-z]
the (...) parentheses will "capture" the match so you can actually do something with it — in my example, I'm just using console.log to output it
the i modifier (at the end of the regexp) means perform a case-insensitive match
the g modifier means match all instances of this regexp in the target string
The while loop will continue running for each match that re.exec finds. Once re.exec cannot match another instance, it will return null and the loop will exit.
Additional information
Try console.log(match) using the code above. Each match comes with other useful information such as the string index where the match occurred
Gotchas
This will not work for nested ${} sets
For example, this regexp will not work on "The quick brown ${fox jumps ${over}} the lazy ${dog}."
You're close!
All you need is to use a + to tell the expression that there will be one or more of whatever was just before it (in this case [a-zA-Z]) like this:
\${[a-zA-Z]+}
A good website for regex reference and testing is http://rubular.com/
It looks like you need to add a +, which tells the regex to look for one or more of a character.
Try: \${[a-zA-Z]+}
You need to use * (zero or more) or + (one or more). So this [a-zA-Z] would be [a-zA-Z]+, meaning 1 or more letters. The entire regex would look like:
\$\{[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.
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&IIgU7OssPIolygXJWtc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+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;
I was trying to do a regex for someone else when I ran into this problem. The requirement was that the regex should return results from a set of strings that has, let's say, "apple" in it. For example, consider the following strings:
"I have an apple"
"You have two Apples"
"I give you one more orange"
The result set should have the first two strings.
The regex(es) I tried are:
/[aA]pple/ and /[^a-zA-Z0-9][aA]pple/
The problem with the first one is that words like "aapple", "bapple", etc (ok, so they are meaningless, but still...) test positive with it, and the problem with the second one is that when a string actually starts with the word "apple", "Apples and oranges", for example, it tests negative. Can someone explain why the second regex behaves this way and what the correct regex would be?
/(^.*?\bapples?\b.*$)/i
Edit: The above will match the entire string containing the word "apples", which I thought is what you were asking for. If you are just trying to see if the string contains the word, the following will work.
/\bapples?\b/i
The regex(es) I tried are:
/[aA]pple/ and /[^a-zA-Z0-9][aA]pple/
The first one just checks for the existence of the following characters, in order: a-p-p-l-e, regardless of what context they are used in. The \b, or word-boundary character, matches any spot where a non-word character and a word character meet, ala \W\w.
The second one is trying to match other characters before the occurrance of a-p-p-l-e, and is essentially the same as the first, except it requires other characters in front of it.
The one I answered with works like following. From the beginning of the string, matches any characters (if they exist) non-greedily until it encounters a word boundary. If the string starts with apple, the beginning of a string is a word-boundary, so it still matches. It then matches the letters a-p-p-l-e, and s if it exists, followed by another word boundary. It then matches all characters to the end of the string. The /i at the end means it's case-insensitive, so 'Apple', 'APPLE', and 'apple' are all valid.
If you have the time, I would highly recommend walking through the tutorial at http://regular-expressions.info. It really goes in-depth and talks about how the regular expression engines match different expressions, it helped me a ton.
To build on #tj111, the reason your second regex fails is that [^a-zA-Z0-9] requires that a character matches; that is, there is some character in that position, and its value is not contained in the set [a-zA-Z0-9]. Markers like \b are called "zero-width assertions". \b, in particular, matches against boundaries between characters or at the beginning or end of a string. Because it is not matching against any character, its "width" is zero.
In sum, [^a-zA-Z0-9] requires a character that does not take a particular value be present, while \b requires only that a boundary be present.
Edit: #tj111 has added most of this to his response. I'm in too late, again :)
This works for apple and apples and its case-insensitive spellings:
var strings = ["I have an apple", "You have two Apples", "I give you one more orange"];
var result = [];
var pattern = /\bapples?\b/i;
for (var i=0; i<strings.length; i++) {
if (pattern.test(strings[i])) {
result.push(strings[i]);
}
}
Your second regex requires a nonalphanumeric character before the first a in apple. "apple" doesn't satisfy this. As others note, "\b" matches not a character, but a word boundary position.
/\bapple/i
\b is a word boundary.
To explain why your attempts do not work, the first one does not check that it is the beginning of the word, so it can have something before it. The second regex you gave says that something must be before the word "apple", but it can't be alphanumeric.