This question already has answers here:
How to put variable in regular expression match?
(3 answers)
Closed 8 years ago.
Sorry If I ask some silly question, but I don't know what to do with it during 2 days.And I need your help.
That's what I want:
var str = "Hello World by Wor";
if(str.match(/\bWor\b/)){
alert('He is here');
}
And it's work, but if I use a Variable:
var str = "Hello World by Wor";
var sear = "Wor";
if(str.match(/\bsear\b/)){
alert('He is here');
}
It doesn't work like example before.
Important: I need to use tags "\b" for make a border for search string.
var str = "Hello World by Wor";
var sear = /Wor/g;
if(str.match(sear).length){
alert("reached")
}
FIDDLE DEMO
NOTE: The g flag is must to get all matches instead of just the first one.
EXPLANATION
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
So I'm trying to make an a sort of auto correct system in my website, and I want to detect a word and if the word is in the image source, then replace it. In my code I am trying to replace the word "Dunk" with SB Dunk. In the source it says Dunk twice, but it only replaces the first time it uses "Dunk", and then keeps making errors and keeps adding more "SB's" to the "Dunk". Heres my code.
//https://stockx-360.imgix.net//Nike-Dunk-Low/Images/Nike-Dunk-Low/Lv2/img01.jpg?auto=format,compress&w=559&q=90&dpr=2&updated_at=1580325806`.replace('%20', '-')
shoesimg.addEventListener('error', function(){
if(shoesimg.src.includes('Dunk')){
const newshoesimg = shoesimg.src.replace(/Dunk/, 'SB Dunk');
shoesimg.src = newshoesimg;
}
You can use replaceAll. Following is the example,
let replacedStr = "1 abc 2 abc 3".replaceAll("abc", "xyz");
// replacedStr is "1 xyz 2 xyz 3"
Another way would be to find the indexes of the word dunk and then using for loop replace one-by-one.
Just add 'g' at the end of regular expression:
shoesimg.addEventListener('error', function(){
if(shoesimg.src.includes('Dunk')){
const newshoesimg = shoesimg.src.replace(/Dunk/g, 'SB Dunk');
shoesimg.src = newshoesimg;
}
https://dmitripavlutin.com/replace-all-string-occurrences-javascript/
Try String.replaceAll():
const newshoesimg = shoesimg.src.replaceAll('Dunk', 'SB Dunk');
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
They send me a text file that starts with unnecessary information and then what is needed goes further. How to remove the beginning to a specific symbol.
Example: line = 'lots of text {text needed}';
It is necessary to delete everything before the symbol {. I tried the regular expression option below:
let str = /^[^{]+/.exec(line)[0];
but it returns the beginning of the text to the symbol {
I need to do the opposite
Thanks for any help
This is how you can do with JavaScript.
var str = 'lots of text {text needed}';
str = str.substring(str.indexOf("{") + 1);
console.log(str);
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to match some paragraphs in Google Docs but the pattern that I wanted to use for it doesn't match the string when run inside a Google Script. However, it works properly on regex101 so I guess I'm missing something. Do you know what?
This is a sample of what I have:
function test() {
var str = "brown fox → jumps over the lazy dog";
var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
var definitionMatch = definitionRe.exec(str); // null
var dummy = "asdf"; // makes the debugger happy to break here
}
When using a string regex such as new RegExp(...), you need to escape your \'s, so then the following:
var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
Will become an escaped version like this:
var definitionRe = new RegExp('([\\w\\s]+)\\s+[\\u2192]\\s+(.+)', 'g');
Otherwise you can do a non string version, but you then can no longer concatenate values to the string (If that is something you would like):
var definitionRe = /([\w\s]+)\s+[\u2192]\s+(.+)/g;
This question already has answers here:
Get string inside parentheses, removing parentheses, with regex
(3 answers)
Closed 9 years ago.
I am trying to extract a string from within a larger string where i need to get the value only inside the brackets.
var str = 'ajay kumar (68766)';
Try this:
var str = 'ajay kumar (68766)';
str = str.slice(str.indexOf('(')+1, str.indexOf(')'));
How about using a regular expression?
var str = 'ajay kumar (68766)';
var output = str.replace(/^[\s\S]*?\((\d+)\)[\s\S]*?$/, '$1');
This question already has answers here:
Remove everything after a certain character
(10 answers)
Closed 9 years ago.
I am trying to remove all characters from a string after a specified index. I am sure there must be a simple function to do this, but I'm not sure what it is. I am basically looking for the javascript equivalent of c#'s string.Remove.
var myStr = "asdasrasdasd$hdghdfgsdfgf";
myStr = myStr.split("$")[0];
or
var myStr = "asdasrasdasd$hdghdfgsdfgf";
myStr = myStr.substring(0, myStr.indexOf("$") - 1);
Use substring
var x = 'get this test';
alert(x.substr(0,8)); //output: get this
You're looking for this.
string.substring(from, to)
from : Required. The index where to start the extraction. First character is at index 0
to : Optional. The index where to stop the extraction. If omitted, it extracts the rest of the string
See here: http://www.w3schools.com/jsref/jsref_substring.asp
I'd recommend using slice as you can use negative positions for the index. It's tidier code in general. For example:
var s = "messagehere";
var message = s.slice(0, -4);