Yet Another document.referrer.pathname Thing - javascript

I'm looking for the equivalent of "document.referrer.pathname". I know there are other questions that are similar to this on SO, but none of them handle all the use cases. For example:
http://example.com/RESULT
http://example.com/RESULT/
http://example.com/RESULT?query=string
All examples should return:
RESULT
or
https://example.com/EXTENDED/RESULT/
EXTENDED/RESULT
Some folks may want the trailing slash included, but I don't because I'm matching against a list of referrers.
I've started with:
document.referrer.match(/:\/\/.*\/(.*)/)[1]
and am struggling adding the query string parsing.
Thanks!

If you have URLs as strings you can create empty anchors and give them the url as href to access the pathname:
var url = 'http://example.com/RESULT?query=string', // or document.referrer
a = document.createElement('a');
a.href = url;
var result = a.pathname.replace(/(^\/|\/$)/g,'');
I set up a test example for you here: http://jsfiddle.net/eWydy/

Try this regular expression:
.match(/\/\/.*?\/(.*?)\/?(\?.*)?$/)[1]
DEMO

If you don't want to create a new element for it or rely on a.pathname, I'd suggest using indexOf and slice.
function getPath(s) {
var i = s.indexOf('://') + 3, j;
i = s.indexOf('/',i) + 1; // find first / (ie. after .com) and start at the next char
if( i === 0 ) return '';
j = s.indexOf('?',i); // find first ? after first / (as before doesn't matter anyway)
if( j == -1 ) j = s.length; // if no ?, use until end of string
while( s[j-1] === '/' ) j = j - 1; // get rid of ending /s
return s.slice(i, j); // return what we've ended up at
}
getPath(document.referrer);
If you want regex though, maybe this
document.referrer.match(/:\/\/[^\/]+[\/]+([^\?]*)[\/]*(?:\?.*)?$/)[1]
which does "find the first ://, keep going until next /, then get everything that isn't a ? until a ? or the last / or end of string and capture it", which is basically the same as the function I did above.

Related

how to retrieve a string between to same charecter

I know how to use substring() but here I have a problem, I'd like to retrieve a number between two "_" from a unknown string length. here is my string for example.
7_28_li
and I want to get the 28. How can I proceed to do so ?
Thanks.
Regex
'7_28_li'.match(/_(\d+)_/)[1]
The slashes inside match make it's contents regex.
_s are taken literally
( and ) are for retrieving the contents (the target number) later
\d is a digit character
+ is "one or more".
The [1] on the end is accesses what got matched from the first set of parens, the one or more (+) digits (\d).
Loop
var str = '7_28_li';
var state = 0; //How many underscores have gone by
var num = '';
for (var i = 0; i < str.length; i++) {
if (str[i] == '_') state++;
else if (state == 1) num += str[i];
};
num = parseInt(num);
Probably more efficient, but kind of long and ugly.
Split
'7_28_li'.split('_')[1]
Split it into an array, then get the second element.
IndexOf
var str = "7_28_li";
var num = str.substring(str.indexOf('_') + 1, str.indexOf('_', 2));
Get the start and end point. Uses the little-known second parameter of indexOf. This works better than lastIndexOf because it is guaranteed to give the first number between _s, even when there are more than 2 underscores.
First find the index of _, and then find the next position of _. Then get the substring between them.
var data = "7_28_li";
var idx = data.indexOf("_");
console.log(data.substring(idx + 1, data.indexOf("_", idx + 1)));
# 28
You can understand that better, like this
var data = "7_28_li";
var first = data.indexOf("_");
var next = data.indexOf("_", first + 1);
console.log(data.substring(first + 1, next));
# 28
Note: The second argument to indexOf is to specify where to start looking from.
Probably the easiest way to do it is to call split on your string, with your delimiter ("_" in this case) as the argument. It'll return an array with 7, 28, and li as elements, so you can select the middle one.
"7_28_li".split("_")[1]
This will work if it'll always be 3 elements. If it's more, divide the length property by 2 and floor it to get the right element.
var splitstring = "7_28_li".split("_")
console.log(splitstring[Math.floor(splitstring.length/2)]);
I'm not sure how you want to handle even length strings, but all you have to do is set up an if statement and then do whatever you want.
If you know there would be 2 underscore, you can use this
var str = "7_28_li";
var res = str.substring(str.indexOf("_") +1, str.lastIndexOf("_"));
If you want to find the string between first 2 underscores
var str = "7_28_li";
var firstIndex = str.indexOf("_");
var secondIndex = str.indexOf("_", firstIndex+1);
var res = str.substring(firstIndex+1, secondIndex);

Find and replace string with dot in javascript

I want to find and replace a part of string if present in string.
String is like '1.png,2.png,3.jpg,4.gif'
I want find 1.png in this string and then replace it if exist with 1.jpg.
I am not able to find it using search() and indexOf() method.
and since i am not able to find it i cannot replace it.
I am trying this way
var str = '1.png'
var new_str = '1.jpg'
var main_str = '1.png,2.png,3.jpg,4.gif';
if(main_str.indexOf(str) > 0){
alert('found')
// now replace it with new_str
}
else{
alert('not found')
}
I have tried following combination but these are not working.
main_str.indexOf('str') > 0
main_str.indexOf(/\str/) > 0
main_str.indexOf(/\"str"/) > 0
main_str.indexOf(str) > 0
Please see and suggest any possible way to do this.
Thanks
The index of 1.png in your string is actually 0, that is why your condition fails. Correct way is to check whether index is not negative, since indexOf returns -1 if substring is not found:
if(main_str.indexOf(str) >= 0){
but even better approach here is to use replace:
main_str.replace(str, new_str)
Since the main_str contains 1.png at index 0, you can never find it with the check main_str.indexOf('str') > 0. Remeber that javascript returns -1 if not found and not zero. So you'll have to update your condition to:
main_str.indexOf('str') != -1
try,
var str="1.png,2.png,3.jpg,4.gif";
var newStr=str.replace(".png",".jpg");
you can use the replace method:
var str = '1.png,2.png,3.jpg,4.gif';
var result=str.replace('1.png','1.jpg')
You can do the replacement using replace :
main_str = main_str.replace(str, new_str)
If you want to replace more than one occurrence, use
main_str = main_str.replace(new RegExp(str,'g'), new_str)
If you want to alert before replacing, do this :
var r = new RegExp(str,'g');
if (main_str.match(r)) {
alert('found')
main_str = main_str.replace(r, new_str)
} else {
alert('not found')
}
you can do this:
var str = "1.png,2.png,3.jpg,4.gif";
var re = /(.png)/i;
var found = str.match(re);
//now ask the user or do whatever else is needed
if (found){
found = confirm("Really overwrite");
}
if (found){
str = str.replace(re, ".jpg");
}
I allow in the string now also upper case or mixed case, like ".Png" or ".PNG"

URL extraction from string

I found a regular expression that is suppose to capture URLs but it doesn't capture some URLs.
$("#links").change(function() {
//var matches = new array();
var linksStr = $("#links").val();
var pattern = new RegExp("^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$","g");
var matches = linksStr.match(pattern);
for(var i = 0; i < matches.length; i++) {
alert(matches[i]);
}
})
It doesn't capture this url (I need it to):
http://www.wupload.com/file/63075291/LlMlTL355-EN6-SU8S.rar
But it captures this
http://www.wupload.com
Several things:
The main reason it didn't work, is when passing strings to RegExp(), you need to slashify the slashes. So this:
"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$"
Should be:
"^(https?:\/\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\/\\w \\.-]*)*\/?$"
Next, you said that FF reported, "Regular expression too complex". This suggests that linksStr is several lines of URL candidates.
Therefore, you also need to pass the m flag to RegExp().
The existing regex is blocking legitimate values, eg: "HTTP://STACKOVERFLOW.COM". So, also use the i flag with RegExp().
Whitespace always creeps in, especially in multiline values. Use a leading \s* and $.trim() to deal with it.
Relative links, eg /file/63075291/LlMlTL355-EN6-SU8S.rar are not allowed?
Putting it all together (except for item 5), it becomes:
var linksStr = "http://www.wupload.com/file/63075291/LlMlTL355-EN6-SU8S.rar \n"
+ " http://XXXupload.co.uk/fun.exe \n "
+ " WWW.Yupload.mil ";
var pattern = new RegExp (
"^\\s*(https?:\/\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\/\\w \\.-]*)*\/?$"
, "img"
);
var matches = linksStr.match(pattern);
for (var J = 0, L = matches.length; J < L; J++) {
console.log ( $.trim (matches[J]) );
}
Which yields:
http://www.wupload.com/file/63075291/LlMlTL355-EN6-SU8S.rar
http://XXXupload.co.uk/fun.exe
WWW.Yupload.mil
Why not do make:
URLS = str.match(/https?:[^\s]+/ig);
(https?\:\/\/)([a-z\/\.0-9A-Z_-\%\&\=]*)
this will locate any url in text

Algorithm (or regular expression) needed to find multiple instances of anything

I'm not sure if there is a simple way of doing this, but is there a way to find multiple instances in an unknown string? For example:
hellohellohellobyebyebyehello
Without knowing the value of the above string, can I return something that will tell me that there are 3 instances of "hello" and 3 instances of "bye" (I'm not worried about the last hello however as I'm looking for consecutive repetition. Thanks in advance!
Maybe the Sequitur algorithm can help: http://sequitur.info/
s = "hellohellohellobyebyebyehello"
s.replace(/(.+)(\1+)/g, function($0, $1) {
console.log($1 + " repeated " + ($0.length / $1.length) + " times");
});
"testhellohellohellobyebyebyehello".match(/(.+)\1+/)
This says : "match a sequence of at least 1 character (.+), then reference that first thing we found \1 at least one time + or more.
It will return ["hellohellohello", "hello"] meaning hellohellohello matches the full expression (expression 0), and "hello" matches expression 1 (the thing we reference with \1).
Caveat:
something like "hahahaha" will yield ["hahahaha", "haha"], instead of ["hahahaha", "ha"]. so you'll need to use the above with some post-processing to get to your desired result.
if you are looking up for dictionary words, you can load your lexicon in a suffix tree,
then consider the characters of your string one by one and go through your tree. Each time your reach a leaf you increment by one the associated "word".
var source = "asdhellohellohellobyehellohellohellohelloasdhello";
var key = "hello";
var len = key.length;
var res = 0, tempres, next;
var last = source.indexOf(key);
while(last != -1)
{
tempres = 0;
next = last;
while(true)
{
tempres++;
next += len;
last = source.indexOf(key, next);
if(last != next)
break;
}
res = (tempres > res) ? tempres : res;
}
console.log(res);//4

Get everything after the dash in a string in JavaScript

What would be the cleanest way of doing this that would work in both IE and Firefox?
My string looks like this sometext-20202
Now the sometext and the integer after the dash can be of varying length.
Should I just use substring and index of or are there other ways?
How I would do this:
// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
A solution I prefer would be:
const str = 'sometext-20202';
const slug = str.split('-').pop();
Where slug would be your result
var testStr = "sometext-20202"
var splitStr = testStr.substring(testStr.indexOf('-') + 1);
var the_string = "sometext-20202";
var parts = the_string.split('-', 2);
// After calling split(), 'parts' is an array with two elements:
// parts[0] is 'sometext'
// parts[1] is '20202'
var the_text = parts[0];
var the_num = parts[1];
With built-in javascript replace() function and using of regex (/(.*)-/), you can replace the substring before the dash character with empty string (""):
"sometext-20202".replace(/(.*)-/,""); // result --> "20202"
AFAIK, both substring() and indexOf() are supported by both Mozilla and IE. However, note that substr() might not be supported on earlier versions of some browsers (esp. Netscape/Opera).
Your post indicates that you already know how to do it using substring() and indexOf(), so I'm not posting a code sample.
myString.split('-').splice(1).join('-')
I came to this question because I needed what OP was asking but more than what other answers offered (they're technically correct, but too minimal for my purposes). I've made my own solution; maybe it'll help someone else.
Let's say your string is 'Version 12.34.56'. If you use '.' to split, the other answers will tend to give you '56', when maybe what you actually want is '.34.56' (i.e. everything from the first occurrence instead of the last, but OP's specific case just so happened to only have one occurrence). Perhaps you might even want 'Version 12'.
I've also written this to handle certain failures (like if null gets passed or an empty string, etc.). In those cases, the following function will return false.
Use
splitAtSearch('Version 12.34.56', '.') // Returns ['Version 12', '.34.56']
Function
/**
* Splits string based on first result in search
* #param {string} string - String to split
* #param {string} search - Characters to split at
* #return {array|false} - Strings, split at search
* False on blank string or invalid type
*/
function splitAtSearch( string, search ) {
let isValid = string !== '' // Disallow Empty
&& typeof string === 'string' // Allow strings
|| typeof string === 'number' // Allow numbers
if (!isValid) { return false } // Failed
else { string += '' } // Ensure string type
// Search
let searchIndex = string.indexOf(search)
let isBlank = (''+search) === ''
let isFound = searchIndex !== -1
let noSplit = searchIndex === 0
let parts = []
// Remains whole
if (!isFound || noSplit || isBlank) {
parts[0] = string
}
// Requires splitting
else {
parts[0] = string.substring(0, searchIndex)
parts[1] = string.substring(searchIndex)
}
return parts
}
Examples
splitAtSearch('') // false
splitAtSearch(true) // false
splitAtSearch(false) // false
splitAtSearch(null) // false
splitAtSearch(undefined) // false
splitAtSearch(NaN) // ['NaN']
splitAtSearch('foobar', 'ba') // ['foo', 'bar']
splitAtSearch('foobar', '') // ['foobar']
splitAtSearch('foobar', 'z') // ['foobar']
splitAtSearch('foobar', 'foo') // ['foobar'] not ['', 'foobar']
splitAtSearch('blah bleh bluh', 'bl') // ['blah bleh bluh']
splitAtSearch('blah bleh bluh', 'ble') // ['blah ', 'bleh bluh']
splitAtSearch('$10.99', '.') // ['$10', '.99']
splitAtSearch(3.14159, '.') // ['3', '.14159']
For those trying to get everything after the first occurrence:
Something like "Nic K Cage" to "K Cage".
You can use slice to get everything from a certain character. In this case from the first space:
const delim = " "
const name = "Nic K Cage"
const result = name.split(delim).slice(1).join(delim) // prints: "K Cage"
Or if OP's string had two hyphens:
const text = "sometext-20202-03"
// Option 1
const opt1 = text.slice(text.indexOf('-')).slice(1) // prints: 20202-03
// Option 2
const opt2 = text.split('-').slice(1).join("-") // prints: 20202-03
Efficient, compact and works in the general case:
s='sometext-20202'
s.slice(s.lastIndexOf('-')+1)
Use a regular expression of the form: \w-\d+ where a \w represents a word and \d represents a digit. They won't work out of the box, so play around. Try this.
You can use split method for it. And if you should take string from specific pattern you can use split with req. exp.:
var string = "sometext-20202";
console.log(string.split(/-(.*)/)[1])
Everyone else has posted some perfectly reasonable answers. I took a different direction. Without using split, substring, or indexOf. Works great on i.e. and firefox. Probably works on Netscape too.
Just a loop and two ifs.
function getAfterDash(str) {
var dashed = false;
var result = "";
for (var i = 0, len = str.length; i < len; i++) {
if (dashed) {
result = result + str[i];
}
if (str[i] === '-') {
dashed = true;
}
}
return result;
};
console.log(getAfterDash("adfjkl-o812347"));
My solution is performant and handles edge cases.
The point of the above code was to procrastinate work, please don't actually use it.
To use any delimiter and get first or second part
//To divide string using deimeter - here #
//str: full string that is to be splitted
//delimeter: like '-'
//part number: 0 - for string befor delimiter , 1 - string after delimiter
getPartString(str, delimter, partNumber) {
return str.split(delimter)[partNumber];
}

Categories

Resources