I've got a JS string
var str = '<at id="11:12345678">#robot</at> ping';
I need to remove this part of a string
<at id="11:12345678">#
So I am trying to use
var str = str.replace("<at.+#","");
But there is no change after excution. Moreover if I try to use match it gives me
str.match("<at.+#");
//Result from Chrome console Repl
["<at id="11:12345678">#", index: 0, input: "<at id="11:12345678">#robot</at> ping"]
So pattern actualy works but replace do nothing
Use // for regex. Replace "<at.+#" with /<at.+#/.
var str = '<at id="11:12345678">#robot</at> ping';
str = str.replace(/<at.+#/,"");
console.log(str);
Documentation for replace
Related
If I have a string
TestString = "{Item:ABC, Item:DEF, Item:GHI}";
How can I remove all of the "Item:"s.
I have tried to use
msg = TestString.replace(/[\Item:/&]+/g, "");
but this unfortunately removes all the Is, Ts. Es and M,s from any letters that may follow.
How can I remove the exact text
Thanks!
It should be simple, you can directly create a Regex like /Item:/g,
var TestString = "{Item:ABC, Item:DEF, Item:GHI}";
var msg = TestString.replace(/Item:/g, "");
console.log(msg);
You could use
/Item:/g
for replacing Item:.
The fomer regular expression
/[\Item:/&]+/g
used a character class with single letters instead of a string.
var TestString = "{Item:ABC, Item:DEF, Item:GHI}",
msg = TestString.replace(/Item:/g, "");
console.log(msg);
Im lost in part of this.
I want to remove the public:// in every link of an image like public://china-taxi_4.jpg
I have tried this but returns null:
var _img = 'public://china-taxi_4.jpg';
var regex = /(public:)(\/\w+)/;
var matches = _img.match(regex);
console.log(matches);
Hope you can help.
I want to remove the 'public://' in every link of an image.
> var img = 'public://china-taxi_4.jpg';
> img.replace(/public:\/\/(?=\S+?\.jpg(?:\s|$))/, "")
'china-taxi_4.jpg'
It removes the word public:// only in the strings which ends with .jpg
You are removing a literal string, not a regular expression. So try:
var _img = 'public://china-taxi_4.jpg';
var result = _img.replace("public://","");
console.log(result);
Regexes are for matching complex expressions.
I think you're missing a slash, try:
var _img = 'public://china-taxi_4.jpg';
var regex = /(public:)(\/\/\w+)/;
var matches = _img.match(regex);
console.log(matches);
From Mozilla Developer Network String.prototype.replace():
Example: Defining the regular expression in replace()
In the following example, the regular expression is defined in
replace() and includes the ignore case flag.
var str = 'Twas the night before Xmas...';
var newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr);
This prints:
'Twas the night before Christmas...'
To match the beginning of a string, use ^
To escape characters that have special meaning in regexp like : and / so that regexp will match these literally, prepend \
This suggests:
var _img = 'public://china-taxi_4.jpg';
var newimg = _img.replace(/^public\:\/\//i, '');
Tested and working in chrome browser console window.
Note: This answer also matches an earlier comment by #dystroy, so I have marked it CW.
var re = /(public:)(\/\/[\w-.]+)/g;
See demo.
http://regex101.com/r/rA7aS3/9
I have the need to return a string (filename) without certain data, example strings are:
eng_somerset_yeovil_montacute-house_962.jpg
eng_south-yorkshire_barnsley_wentworth-castle_0.jpg
eng_staffordshire_harriseahead_mow-cop-castle_1329317.jpg
eng_somerset_weston-super-mare_marine-lake-walkway_29113.jpg
These example strings need to be returned as the following:
eng_somerset_yeovil_montacute-house.jpg
eng_south-yorkshire_barnsley_wentworth-castle.jpg
eng_staffordshire_harriseahead_mow-cop-castle.jpg
eng_somerset_weston-super-mare_marine-lake-walkway.jpg
I've tried using the regex below, but i only see the pattern and after the pattern returned:
filename = fl.replace(/(^.*?(?=[_]{1}[0-9]{1,10}))/gi, '');
_962.jpg
_0.jpg
_1329317.jpg
_29113.jpg
Thanks for your help.
This regex should work:
var repl = str.replace(/_\d+(?=\.jpg$)/, "");
TESTING:
str = 'eng_somerset_yeovil_montacute-house_962.jpg';
var repl = str.replace(/_\d+(?=\.jpg$)/, "");
// eng_somerset_yeovil_montacute-house.jpg
I have a string in which I want to replace an occurence which I am not being able to achieve following is the code
var code="user_1/has some text and user_1? also has some text";
newcode=code.replace(/user_1//g,'_');
One more thing if i have to replace a string from another string how to do?
example.
var replacestring="user_1";
var code="user_1/some value here for some user";
var newcode=code.replace(/+replacestring+/g,'_');
/ is a special char thus needs to be escaped with \ before it:
var code = "user_1/has some text and user_1? also has some text";
var newcode = code.replace(/user_1\//g, '_');
alert(newcode);
Live DEMO
if you want to replace all user_1, use this:
var code = "user_1/has some text and user_1? also has some text";
var newcode = code.replace(/user_1/g, '_');
alert(newcode);
Live DEMO
Escape the / in the regex using \
newcode=code.replace(/user_1\//g,'_');
For your comment
#Vega I have another confusion. can i use a value in a string to pass
instead of user_1/ for replacement? what would be the syntax?
You can initialize RegEx object like below,
var userName = 'user_1/';
var newcode = code.replace(new RegExp(userName, 'g'), '_');
Read More about RegEx
I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.
If you want to strip out things that are digits, a regex can do that for you:
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"
(\d is the regex class for "digit". We're replacing them with nothing.)
Note that as given, it will remove any digit anywhere in the string.
This can be done in JavaScript:
/^[^\d]+/.exec("foobar1")[0]
This will return all characters from the beginning of string until a number is found.
var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));
Find some more information about regular expressions in javascript...
This should do what you want:
var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");
This replaces al numbers in the entire string. If you only want to remove at the end then use this:
var re = /[0-9]*$/g;
I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.
var yourString = "foobar1, foobaz2, barbar23, nobar100";
var yourStringMinusDigits = yourString.replace(/\d/g,"");