I've got the following code:
var one = "What a lovely puppy!
Is it
a beagle?",
two = "You've got a lovely funny beagle!",
three = "Where's my lovely dog? Where's my little beagle?";
var target = (".* lovely(?!funny).* beagle.*");
What I need is to "catch" words between lovely and beagle. If there's word funny between my "catch" boundaries (var two), the method
match() should ignore it.
In other words, it should check for matches all phrases from lovely to beagle, which involve no word funny.
My var target seems to be written incorrect.
I expect:
one.match(target) //return: "puppy! Is it a"
two.match(target) //must be ignored, because of word "funny"
three.match(target) //return: "dog? Where's my little"
Any help will be appreciated!
You'll have to join the repeated test for funny and the .*. Try
lovely(?:(?!funny).)*beagle
Check it out here at regex101.
Regards
I suggest to make a per character check for the first letter, then check for 'funny' if current letter is an inital f:
var regex = /\blovely\b([^f]|\Bf|f(?!unny\b))+\bbeagle\b/;
Related
Is there a way to return all words in a given string? The best solution I have currently found is using the match method and returning any string with at least one non-whitespace char (/\S+/g).
The issue with this method is that it includes a comma, period, etc. in the word. If I try using a RegExp with \w, then it doesn't include periods and commas, but it makes "don't" two words because of the '.
Is there any true and easy solution to this issue?
For example: "I don't want to go, mom". This should return the words [I, don't, want, to, go, mom]
Would this work?
mystr.replace(".","").split(/\s/g);
I would have commented, but I don't have 50 rep
Use word boundaries in regex and match function
const matches = "I don't want to go, mom.".match(/(\b[^\s]+\b)/g);
console.log(matches);
I don't know if this is exactly what are you looking for but... Using just the example string you've provided this worked:
myString = "I don't want to go, mom"
wordsArray = []
myString = myString.replace(',', '')
wordsArray = myString.split(' ')
console.log({wordsArray})
But be ware that you have so much additional cases:
"two-handed" there is one or two words? ['two', 'handed'], ['two-handed'] or ['twohanded']
"Mrs. Foo", "Dr. bar"... expecting: ['Mrs', 'Foo'], ['Mrs. Foo'], ['Mrs.Foo'], ['MrsFoo'] ?
I'll appreciate any feedback.
Can someone help me on this regex, i need to replace the text which match with text with the same length.
"StateQuestion".replace(/State/gi, 'YES')
As per the regex, it searches for state and replaces with YES and the output is YESQuestion.
But I need to have the regex to detect the exact word State and not with StateQuestion. How to modify this script, please help on here
Put word boundaries around the individual word you want to target:
console.log("StateQuestion".replace(/\bState\b/gi, 'YES'));
console.log("New York is a lovely State.".replace(/\bState\b/gi, 'YES'));
Here is my code for your problem. I think this will work fine
var str = "State StateQuestion State state";
var p = /\bState\b/gi;
mod_str = str.replace(p, "Yes");
console.log(mod_str)
I am trying to write a regex to extract the last few words from a sentence. However, I can't seem to make it work.
var str = "Tell me about robin hood";
const TELL_ME_ABOUT_REGEX = /^Tell me about (\w+\s*)*/
if( str.match(TELL_ME_ABOUT_REGEX) ){
var matches = TELL_ME_ABOUT_REGEX.exec( str );
console.log( "user wants to know about ",matches);
}
I am trying to get the word "robin hood". But I only end up with "hood".
[ 'Tell me about robin hood',
'hood',
index: 0,
input: 'Tell me about robin hood' ]
What do I change in my regex?
Why you need regex for this? You can do it without regex like it
var str = "Tell me about robin hood";
var str = str.split(" ").splice(3).join(" ");
alert(str);
http://codepen.io/anon/pen/aNvrYz
Edit : Regex Solution
var str = "Tell me about robin hood";
var match = str.match(/^Tell me about (.*)/i);
var textInDe = match[1];
alert(textInDe);
http://codepen.io/anon/pen/BKoerY
Here is a correct regex:
^Tell me about ((?:\w+\s*)*)
Compare to your original one which is
^Tell me about (\w+\s*)*
That's so closed. The point is you should use the non-capturing group for an inner bracket and capturing group for an outer bracket.
Note that (\w+\s*)* from your regex, it might captures robin to \1 at the first time and then overwrites hood to \1. So, now you might understand that regex engine will work in this way.
The problem with /^Tell me about (\w+\s*)*/ is that are multiples matches for "Tell me about robin hood", i.e., robin is a possible match, as well robin\s and so forth. The repetition characters may confuse you sometimes, but when you think on all match possibilities, it can be clearer.
For matching all that came after Tell me about you can simply get it all at once, with only one possible match: (.*).
Regex demo
Try this
Tell me about ([\w\s]+)
Regex Demo
(\w+\s*)* will capture 'robin' then 'hood' but only keep the last iteration => 'hood'
I'm thinking that this is something very simple, but I can't find an answer anywhere online. I've found results on how to match the whole first line in a multiline string, but not how to find all occurrences of a certain character ONLY on the first line.
So for instance:
HelloX dXudXe
How areX yXou?
FxIXne?
Matching all capital Xs only on the first line, and replacing that with nothing would result in:
Hello dude
How areX yXou?
FxIXne?
This matches only the first X:
/X/m
This matches all Xs:
/X/g
So I'm guessing the answer is the regex version of one of these statements:
"Replace all X characters until you find a newline"
"Replace all X characters in the first line"
This sounds like such a simple task, is it? And if so, how can it be done? I've spent hours looking for a solution, but I'm thinking that maybe I don't get the regex logic at all.
Without knowing the exact language you are using, it's difficult to give an example, but the theory is simple:
If you have a complex task, break it down.
In this case, you want to do something to the first line only. So, proceed in two steps:
Identify the first line
Perform an operation on it.
Using JavaScript as an example here, your code might look like:
var input =
"HelloX dXudXe" + "\n" +
"How areX yXou?" + "\n" +
"FxIXne?";
var result = input.replace(/^.*/,function(m) {
return m.replace(/X/g,'');
});
See how first I grab the first line, then I operate on it? This breaking down of problems is a great skill to learn ;)
Split the string into multiple lines, do the replacement on the first line, then rejoin them.
var lines = input.split('\n');
lines[0] = lines[0].replace(/X/g, '');
input = lines.join('\n');
I'm trying to fix some typos and one common one is a space missing betweens sentences: "This is a sentence.Here is another sentence." I want to match and add a space so I wrote this regular expression:
var re = /\.(?=[A-Z]|\()/g;
var res = str.replace(re, '. ');
That covers the squished together sentences, as well as another typo involving parenthesis which is not important for this question.
The problem is that there are acronyms that show up, which are also matched and (incorrectly) replace. Example: "The U.S. is a country" is replaced to "The U. S. is a country". I'm trying to prevent these acronyms from being matched. I think maybe what I want is a "lookbehind", but javascript doesn't support that.
Any idea how to solve this?
You could try:
\.(?=[A-Z]|\()(?![A-Z]\.)
This ensures the proceeding characters after the "." do not include a capital letter followed by a "."
This seems to work:
var str = "A sentance.Another sentance with an A.C.R.O.N.Y.M.Yet another sentence."
var re = /\.(?=[A-Z][^.]|\()/g;
var res = str.replace(re, '. ');
res // => "A sentance. Another sentance with an A.C.R.O.N.Y.M. Yet another sentence."