match end of line javascript regex - javascript

I'm probably doing something very stupid but I can't get following regexp to work in Javascript:
pathCode.replace(new RegExp("\/\/.*$","g"), "");
I want to remove // plus all after the 2 slashes.

Seems to work for me:
var str = "something //here is something more";
console.log(str.replace(new RegExp("\/\/.*$","g"), ""));
// console.log(str.replace(/\/\/.*$/g, "")); will also work
Also note that the regular-expression literal /\/\/.*$/g is equivalent to the regular-expression generated by your use of the RegExp object. In this case, using the literal is less verbose and might be preferable.
Are you reassigning the return value of replace into pathCode?
pathCode = pathCode.replace(new RegExp("\/\/.*$","g"), "");
replace doesn't modify the string object that it works on. Instead, it returns a value.

This works fine for me:
var str = "abc//test";
str = str.replace(/\/\/.*$/g, '');
alert( str ); // alerts abc

a = a.replace(/\/\/.*$/, "");

Related

jQuery / Javascript replace multiple occurences not working

I'm trying to replace multiple occurrences of a string and nothing seems to be working for me. In my browser or even when testing online. Where am I going wrong?
str = '[{name}] is happy today as data-name="[{name}]" won the match today. [{name}] made 100 runs.';
str = str.replace('/[{name}]/gi','John');
console.log(str);
http://jsfiddle.net/SXTd4/
I got that example from here, and that too wont work.
You must not quote regexes, the correct notation would be:
str = str.replace(/\[{name}\]/gi,'John');
Also, you have to escape the [], because otherwise the content inside is treated as character class.
Updating your fiddle accordingly makes it work.
There are two ways declaring regexes:
// literal notation - the preferred option
var re = /regex here/;
// via constructor
var re = new Regexp('regex here');
You should not put your regex in quotes and you need to escape []
Simply use
str = str.replace(/\[{name}\]/gi,'John');
DEMO
While there are plenty of regex answers here is another way:
str = str.split('[{name}]').join('John');
The characters [ ] { } should be escaped in your regular expression.

Remove all occurrences of text within string

Say I had a string in JavaScript that looked like this:
var str = "Item%5B9%5D.Something%5B0%5D.Prop1=1&Item%5B9%5D.Something%5B0%5D.Prop2=False&Item%5B9%5D.Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B1%5D.Prop1=2&Item%5B9%5D.Something%5B1%5D.Prop2=False&Item%5B9%5D.Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B2%5D.Prop1=3&Item%5B9%5D.Something%5B2%5D.Prop2=False&Item%5B9%5D.Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B3%5D.Prop1=4&Item%5B9%5D.Something%5B3%5D.Prop2=False&Item%5B9%5D.Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00"
and wanted it to look like this:
var str = "Something%5B0%5D.Prop1=1&Something%5B0%5D.Prop2=False&Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Something%5B1%5D.Prop1=2&Something%5B1%5D.Prop2=False&Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Something%5B2%5D.Prop1=3&Something%5B2%5D.Prop2=False&Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Something%5B3%5D.Prop1=4&Something%5B3%5D.Prop2=False&Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00"
i.e. remove all of the Item%5BX%5D. parts
How would I go about doing this? I thought of using something like:
str = str.substring(str.indexOf('Something'), str.length);
but obviously that only removes the first occurrence.
Also the number in-between the %5B and %5D could be anything, not necessarily 9.
This seems like something that should be simple but for some reason I'm stumped. I found a few similarish things on SO but nothing that handled all the above criteria.
You could use a regular expression :
str = str.replace(/Item[^.]+\./g, '');
or if you want something more precise because you'd want to keep Item%6B3%4D :
str = str.replace(/Item%5B.%5D\./g, '');
str = str.replace('Item%5B9%5D', '');
EDIT: Missed the part where 9 in the string could be any number. You can use:
str = str.replace(/Item%5B\d%5D\./g, '');
Avoid using a regular expression where complex "needle" escaping is required:
var str = "something complex full of http://, 'quotes' and more keep1 something complex full of http://, 'quotes' and more keep2 something complex full of http://, 'quotes' and more keep3"
var needle = "something complex full of http://, 'quotes' and more";
while( str.indexOf(needle) != '-1')
str = str.replace(needle,"");
document.write(str);
Outputs:
keep1 keep2 keep3
Here you go:
str = str.replace(/Item%5B\d%5D\./g,'');
Live Demo
Try using regular expressions:
str = str.replace(/Item%5B[^.]*%5D./g, '');
This assumes that you can have anything of any length between %5B and %5D.
JSFiddle
Using split() & join() method
var str = "Item%5B9%5D.Something%5B0%5D.Prop1=1&Item%5B9%5D.Something%5B0%5D.Prop2=False&Item%5B9%5D.Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B1%5D.Prop1=2&Item%5B9%5D.Something%5B1%5D.Prop2=False&Item%5B9%5D.Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B2%5D.Prop1=3&Item%5B9%5D.Something%5B2%5D.Prop2=False&Item%5B9%5D.Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B3%5D.Prop1=4&Item%5B9%5D.Something%5B3%5D.Prop2=False&Item%5B9%5D.Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00";
console.log(str.split(/Item%5B\d%5D\./g).join(''));

I have a string and want to remove special chars in its specific part using javascript

I have a string looks like:
var myString= '{"key1":"value1", "key2":"value2", "key3":"value3"}';
What I want is to remove double qoutes and other special chars only from value3 ... for example some times we have string looks like:
var myString= '{"key1":"value1", "key2":"value2", "key3":"This is "value" with some special chars etc"}';
Please note I do not want to remove double quotes after colon and before This and similarly I do not want to remove last double quotes. I just want to modify string so it will become:
var myString = '{"key1":"value1", "key2":"value2", "key3":"This is value with some special chars etc"}';
Simple...
var myNewString = eval( '('+ myString +')');
var toBeEscaped = myNewString.key3;
var escapedString = doYourCleanUpStuff(toBeEscaped);
myNewString.key3 = escapedString;
myNewString = JSON.stringify(myNewString);
===============
What did I do?
1) Your string looks like valid JSON.
2) Cast it into JSON object. (using eval() )
3) Retrieve value of key3 and catch it in some temp variable.
4) Do cleanup things with this temporary variable.
5) assign cleaned up temporary variable back to key3.
6) Stringify the JSON.
Don't go around beating bush with array manipulation and split functions, especially when there are simpler ways :)
You will probably want to use the Array map function in conjunction with the string replace function.
To use the replace function, you'll probably want to use regular expressions. For example, the following regular expression will remove double quotes not immediately after a colon: [^:]".
To replace every quote not immediately followed by a colon in an array of strings, you could write this:
var myString= '{"key1":"value1", "key2":"value2", "key3":"value3"}';
var modified = myString.map(function(s) { return s.replace(/[^:]"/g, ''); });
Make your own modifications to the regular expression to match the characters you want to remove.
Refer to http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/.
First thing that comes to my mind is to isolate the Value you want.
You can .Split() for the value you want
var myString = '{"key1":"value1", "key2":"value2",
"key3":"value3"}';var n=str.split(" ");
var key3Value = str.split(//Based on your value);
//Run .replace() on your key3Value to replace your doublequotes, etc.
//Re-declare myString to include the updated key3Value
Sorry, no time at the moment to finish the code, and I am sure their are better/shorter answers than mine. Just wanted to include this as an option.

Javascript RegEx global string search for underscore character(_)

I am horrible with RegEx and I have been using this online tester for some time now and still can not find what I need.
So I have the string "2011_G-20_Cannes_summit". I want to replace all the underscores (_) with spaces.
So I want something like this:
var str = "2011_G-20_Cannes_summit";
str.replace(/_/g," "); or str.replace(/\_/g);
Though neither is working...
What am I missing?
That works fine. The replace method doesn't modify the existing string, it creates a new one. This will do what you want:
var str = "2011_G-20_Cannes_summit";
str = str.replace(/_/g," ");

How would I get a certain part of a string?

Here's an example string:
survey_questions_attributes_1317784471568_answers_attributes
I want to get the 1317784471568 out of the string.
But that part could also be text. For example, I may also need to get a string called new_questions from that same spot in the string.
The constant here is that survey_questions_attributes_ will always precede the chunk I want and _answers_attributes will always follow it.
id = str.match(/_questions_attributes_(.+)_answers_attributes/)[1];
var str = "survey_questions_attributes_1317784471568_answers_attributes";
var newStr = str.replace("survey_questions_attributes_", "");
newStr = newStr.replace("_answers_attributes", "");
Easiest way I can think of without using regular expressions or the like.
Try -
var text = "survey_questions_attributes_1317784471568_answers_attributes"
.replace("survey_questions_attributes_","")
.replace("_answers_attributes","")
Use substring():
mystring = "survey_questions_attributes_1317784471568_answers_attributes"
newstring = mystring.substring(28, 41)

Categories