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)
Related
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/;
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 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."
First of all, I am NOT good at javascript or jquery, so I thought to search in the internet first, like I always do for things that I do not know. But I could not find anything about my question, so I thought to ask for help here and I hope you can help me.
When I started to ask the question, this website told me to share my research, but I do not have what to share.
My question is: "How to find that how many times a specific word is written in one textarea using javascript or jquery?"
I am not sure if that can be done with javascript or jquery, that's why I wrote both those.
Thanks
A simple proof-of-concept:
$('#test').keyup(function(e){
var v = $(this).val(), // the current value of the textarea,
w = v.split(/\s/), // the individual words
needle = 'img', // what you're looking for
c = 0; // the count of that particular word
for (var i=0,len=w.length;i<len;i++){
// iterating over every word
if (w[i] === needle){
// if a given word is equal to the word you're looking for
// increase the count variable by 1
c++;
}
}
// set the text of the 'output' element to be the count of occurrences
$('#output').text(c);
});
JS Fiddle demo.
References:
keyup().
String.split().
text().
val().
You can use .match() to match regular expressions in a string.
var str = "This my wordy string of words";
console.log(str.match(/word/g).length); // Prints 2 as it's matched wordy and words
console.log(str.match(/word\b/g).length); // Prints 0 as it has NOT matched wordy and words due to the word boundary
These are also case sensitive. You may want to study up RegExp's for additional options.
Try
var regex = new RegExp('\\b' + word + '\\b', 'gi');
var count = string.match(regex).length
Okay I have a simple Javascript problem, and I hope some of you are eager to help me. I realize it's not very difficult but I've been working whole day and just can't get my head around it.
Here it goes: I have a sentence in a Textfield form and I need to reprint the content of a sentence but WITHOUT spaces.
For example: "My name is Slavisha" The result: "MynameisSlavisha"
Thank you
You can replace all whitespace characters:
var str = "My name is Slavisha" ;
str = str.replace(/\s+/g, ""); // "MynameisSlavisha"
The /\s+/g regex will match any whitespace character, the g flag is necessary to replace all the occurrences on your string.
Also, as you can see, we need to reassign the str variable because Strings are immutable -they can't really change-.
Another way to do it:
var str = 'My name is Slavisha'.split(' ').join('');