I've currently got the following to remove spaces and replace them with a hyphen.
How can I go about replacing a space with a hyphen and a dot with nothing and keep it in the same variable?
url = url.replace(/\s/g, '-');
url = url.replace(/\s/g, '-').replace(/\./g, ''); might do it.
I use this:
// This little gadget does replace for all not just first occurence like the native javascript function.
String.prototype.replaceAll = function(strTarget, strSubString){
var strText = this;
var intIndexOfMatch = strText.indexOf(strTarget);
while (intIndexOfMatch != -1){
strText = strText.replace(strTarget, strSubString);
intIndexOfMatch = strText.indexOf(strTarget);
}
return(strText);
}
Related
In my Javascript code, I get one very long line as a string.
This one line only has around 65'000 letters. Example:
config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...
What I have to do is replace all & with an break (\n) first and then pick only the line which starts with "path_of_code=". This line I have to write in a variable.
The part with replace & with an break (\n) I already get it, but the second task I didn't.
var obj = document.getElementById('div_content');
var contentJS= obj.value;
var splittedResult;
splittedResult = contentJS.replace(/&/g, '\n');
What is the fastest way to do it? Please note, the list is usually very long.
It sounds like you want to extract the text after &path_of_code= up until either the end of the string or the next &. That's easily done with a regular expression using a capture group, then using the value of that capture group:
var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
var text = match[1];
}
Live Example:
var theString = "config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...";
var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
var text = match[1];
console.log(text);
}
Use combination of String.indexOf() and String.substr()
var contentJS= "123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...";
var index = contentJS.indexOf("&path_of_code"),
substr = contentJS.substr(index+1),
res = substr.substr(0, substr.indexOf("&"));
console.log(res)
but the second task I didn't.
You can use filter() and startsWith()
splittedResult = splittedResult.filter(i => i.startsWith('path_of_code='));
I'm doing a coding challenge that wants us to create a function that finds and replaces a word in a sentence. I define the reg expression like this
//"before" is the parameter with the word to be replaced
var regRep = '/'+before+'/gi';
and I'm using it like this
//"str" is the sentence to search and prepAfter" is a variable with the replacement word.
var newStr = str.replace(regRep, prepAfter);
when returning newStr I get the original str without any modifications. I went through and console.log()ed each of my variables and chunks of logic and the replace() method is the only thing not working as it's suppose to. Here's the entire function.
function myReplace(str, before, after) {
var prepAfter = "";
var caseCheck = before.charAt(0);
var regRep = '/'+before+'/gi';
if(caseCheck === caseCheck.toUpperCase()){
var firstLetter = after.substr(0,1).toUpperCase();
var wordLength = after.length -1;
var remWord = after.substr(1,wordLength);
prepAfter = firstLetter.concat(remWord);
}
else{ prepAfter = after; }
var newStr = str.replace(regRep, prepAfter);
return newStr;
}
What am I missing?
var regRep = new RegExp(before, 'gi');
If you pass a string to replace() (as you did), it will look for the actual string.
Note: if before is just a word in your case, you might not even need a regex, just passing it to replace() as-is could do. Depends on whether or not you need to check things like whitespace before and after.
i have a string,
mystr = 'public\uploads\file-1490095922739.jpg';
i want to replace
public\uploads
with " ", so that i just want to extract only file name ie
file-1490095922739.jpg
or like,
\uploads\file-1490095922739.jpg
how can i do this, is there any methods for this in js or can we do it by replace method.
i am performing the following steps,
var imagepath1;
var imagepath = 'public\uploads\file-1490095922739.jpg';
unwantedChar = 'public|uploads';
regExp = new RegExp(unwantedChar , 'gi');
imagepath = imagepath.replace(regExp , '');
imagepath1 = imagepath;
$scope.model.imagepath = imagepath1.replace(/\\/g, "");
please suggest me optimized method.
var input = "public\\uploads\\file-1490095922739.jpg";
var result = input.replace("public\\uploads\\", "");
This is what you're looking for, no need for fancy regexs :). More information about replace can be found here.
Maybe I don't understand the issue - but wouldn't this work?
var mystr = 'public\uploads\file-1490095922739.jpg';
var filename = mystr.replace('public\uploads', '');
If you want to get the part of the string after the last backslash character, you can use this:
var filename = mystr.substr(mystr.lastIndexOf('\\') + 1);
Also note that you need to escape the backslash characters in your test string:
var mystr = 'public\\uploads\\file-1490095922739.jpg';
What about just doing:
var imagepath = 'public\\uploads\\file-1490095922739.jpg';
$scope.model.imagepath = imagepath.replace('public\\uploads\\', '');
instead of using a bunch of unnecessary variables?
This way you're getting the file path, removing public\uploads\ and then setting the file path to $scope.model.imagepath
Note that this will only work if the image file path always matches 'public\uploads\*FILENAME*'.
var url = '/anysource/anypath/myfilename.gif';
var filename = url.slice(url.lastIndexOf('/')+1,url.length);
Search for the last forward slash, and slice the string (+1 because you don't want the slash), with the length of the string to get the filename. This way, you don't have to worry about the path is at all times.
How do I take each of these and combine them into a single regEx expression?
var t = "<test>";
t = t.replace(/^</, '');
t = t.replace(/>+$/, '');
which results in t equal to test without the <>
You can use pipe |. In regex it means OR:
t = "<test>>".replace(/^<|>+$/g, "");
// "test"
But of course, you can use another ways like:
t = "<test>>".replace(/[<>]/g, "");
// "test"
or even with match:
t = "<test>>".match(/\w+/)[0];
// "test"
Make sure you've added the g-global flag when needed. This flag stands for all occurrences.
If I understand correctly you only want to replace beginning '<' symbols and ending '>' symbols, try this one [>$^<]
var t = "<test>";
t = t.replace('/[>$^<]/', '');
Try this
t = t.replace(/^</, '').replace(/>+$/, '');
If you want it in single line, use | to combine the regex.. Like this
t = t.replace(/^<|>+$/g, "");
or capture groups like this
t = t.replace(/^<(.*)>+$/g, '$1');
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