I am trying to recognize a string like this one: file=2013/08/something_320x480.jpg and to replace it in JavaScript.
Here is my regex:
newStr = str.replace('/file=\d+\/\d+\/.+\d+x\d+.jpg/', 'irrelevant');
I also tried
newStr = str.replace('/file=.+\.jpg/', 'irrelevant');
However, my string is never replaced. What am I doing wrong?
The regexp literal does not take apostrophes.
Try:
newStr = str.replace(/file=\d+\/\d+\/.+\d+x\d+.jpg/, 'irrelevant');
Are you sure the file is set as you say it is? I just tried your example in the console and it works...
> var a = "file=2013/08/something_320x480.jpg"
undefined
> a.replace(/^file=\d+\/\d+\/.+\d+x\d+.jpg$/, 'irrelevant');
"irrelevant"
Update: I didn't spot that you had apos' in your regex, well spotted #Taemyr
Related
I've searched all around the web for a response to this question,
Why do my .replace only replace chars that doesnt have any other char behind
My code is :
const fixedstring = string.replace('&', '^&')
When i enter : ¬epad.exe& has a string, it give me the output ^¬epad.exe& instead of ^¬epad.exe^&
Can someone help me ? (i've tried the /g)
Thanks for the guy that will help me or just read my post, thanks !
You can't use the global flag /g unless you are using regex to find the characters to replace. You are currently using a string to find, which will only match the first instance.
Instead, use the regex:
.replace(/&/g, "^&")
Here is a working example.
const stringToReplace = "¬epad.exe&"
const replaced = stringToReplace.replace(/&/g, '^&')
console.log(replaced)
If I have the following string: table.row.columns.values.many. I am looking to add character * after values.
So expected output would be: table.row.columns.values*.many .
If there is no "values" in string then string should stay the same.
Please use this code.
let str = "table.row.columns.values.many"
str = str.replace("values", "values*");
console.log(str);
You can use also "replaceAll" if you want to replace all "values".
I think this works nice for you, really simple, and easy way
var str = "table.row.columns.values.many";
str = str.replace("values", "values*")
console.log(str)
I have spent several hours trying to figure out what is going on here looking at stack overflow and elsewhere and I cannot figure out what is going on. I would really appreciate any help!!
I need to make document.write('< /div>'); go to -> < /div>
I've simplified it down to the simplest possible case with the html example below.
<script>
var str = "document.write('</div>');";
str = str.replace("/document.write/g","");
console.log(str); //</div>
</script>
Exclude the quotes and it works. It is being interpreted as a string literal because of the quotes, whereas a regular expression literal is expressed between plain /s.
Also, . needs to be escaped or it matches any other single character.
<script>
var str = "document.write('</div>');";
str = str.replace(/document\.write/g,"");
console.log(str); //</div>
</script>
String.prototype.replace() accepts either a string or a regex. If you are going with a string this should be:
var str = "document.write('</div>');";
str = str.replace("document.write","");
console.log(str);
I'm trying to match #(\w+) in a div content and remove it.
Here's what i've tried : http://jsfiddle.net/mxgde6m7/1/ .
#(\w+) works , but it doesn't replace with space.
var content = document.getElementById('contentbox');
var find = '#(\w+)';
var reg = new RegExp(find, 'g');
var result = content.innerHTML.replace(reg, ' ');
alert(result);
<div id="contentbox">#d test
What i want: <div id="contentbox">test
</div>
Thanks in advance.
EDIT
Okay, one problem solved, another one came up.
My script http://jsfiddle.net/mxgde6m7/9/ works perfectly there, but when i try it on my website, only a half works. The last part where it should replace #(\w+) with space doesn't work at all. If i copy/paste the CONTENT of the function in console(chrome), it works , but if i paste the function and i call it, it doesn't work.
Please help ! I'm stuck.
Using a RegExp constructor, you need two backslashes \\ in place of each backslash \.
var find = '#(\\w+)';
hwnd is correct that you need to double escape \w in your regular expression.
var find = '#(\\w+)';
But, you could also make this code much cleaner by defining a regex literal like so -
var content = document.getElementById('contentbox');
var result = content.innerHTML.replace(/#(\w+)/g, ' ');
alert(result);
Doing it this way doesn't require double escaping, as it's not a string.
I have a string coming back from a webservice the contains open brackets like such "[]"
so the string would look like something like this:
[1] blabh balh blah
I would like to write a regexp that would remove the "[1]" or anything between open brackets.
Right now I've tried something like:
var regexp = /\[[]\\]/g;
but this does not work. I'm stumbling on my own two feet here.
I simply just want to find anything that starts with "[" and ends with "]" and replace everything in the middle including the open and closed brackets.
Any help or guidance would be much appreciated.
This should work:
var str = '[1] blabh balh blah';
str = str.replace(/\[.*?\]\s?/g, '');
If you have nested brackets regexp might no be the best option though.
Is using regexp a requirement? If not, a simple solution might be:
var myString = '[1] Bob Loblaw is the man';
myString = myString.slice(myString.indexOf(']')+1);