How to insert value after specific text in the string - javascript

I would like to add value to my URL after the specific text. How I can achieve this?
I found a solution where I can slice and add values but I don't have the index fixed in this URL.
const url = 'www.website.com/api/test1/test2';
const url = 'www.website.test.com/api/test1/test2/test3';
const output1 = 'www.website.com/api/ha/test1/test2';
const output2 = 'www.website.test.com/api/ha/test1/test2/test3';

If you want to add a certain value after a certain string you can simply replace the known value with the new string:
url.replace('/api', '/api/ha')

If you don't have a fixed index, you should use Regex to search the string. Could look like this:
/(www.\D+\/api\/)[\D\d\/]+/gm
This code would put the before and after into a group and you can then do
group1 + yourstring + group2
to add it together.

I prefer working with URL
const url1 = new URL('https://www.website.com/api/test1/test2');
const pn = url1.pathname.split("/")
pn.splice(2,0,'ha')
url1.pathname=pn.join("/");
console.log(url1.href)

Related

Javascript extract string from start to 0/, removing anything after 0/

I have a string which looks like this:
file:///storage/emulated/0/Pictures/IMG212.jpg
I want to remove anything from Pictures onwards so I would get this:
file:///storage/emulated/0/
How can I do this?
var regex = /^.*0\//
var matches = str.match(regex);
console.log(matches[0]);
You could find the index of /0 and use substring to select everything up to that point, like so:
const line = "file:///storage/emulated/0/Pictures/IMG212.jpg";
// Get the location of "/0", will be 24 in this example
const endIndex = line.indexOf("/0");
// Select everything from the start to the endIndex + 2
const result = line.substring(0, endIndex+2);
The reason why we add +2 to the endIndex is because the string that we're trying to find is 2 characters long:
file:///storage/emulated/0/Pictures/IMG212.jpg
[24]--^^--[25]
You can split the string using split method:
const myString = "file:///storage/emulated/0/Pictures/IMG212.jpg";
const result = myString.split("0/")[0] + "0/";
console.log(result); // file:///storage/emulated/0/
documentation for split method on MDN

How can I trim a string if input is Ramesh-123-india ...my desire output should be only "india""

In material table one of the column has to be trim ..for example input is ramesh-123-india ,the output should be come as only "india"....how to do this
1) You can make use of split here
const str = "ramesh-123-india";
const result = str.split("-")[2];
console.log(result);
2) You can also use regex /\w+$/ here as:
const str = "ramesh-123-india";
const result = str.match(/\w+$/)[0];
console.log(result);
Consider looking into the split().
JavaScript docs
You can pass in a string pattern that it searches for to split it up.

What's the best way to increment a string [jQuery]

I have a variable that returns me the url of an image, and in this url the id of the image can vary the quantity of numbers... what would be the best way to complement the id like this:
As it is: http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg
How to stay: http://mydomain.com.br/arquivos/ids/1318090-500-500/94842_1.jpg
In short, I need to add the "-500-500" into id
Sorry... A Example:
var a = "http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg";
var b = "-500-500";
var position = 43;
var output = [a.slice(0, position), b, a.slice(position)].join('');
console.log(output);
You can do something like following:
var url = "http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg";
var index = url.lastIndexOf("/");
var new_url = url.slice(0, index) + "-500-500" + url.slice(index);
link = link.split("/").reduce((r,c,i)=>r+c+i===5?"-500-500": "","").join("/");
Simply add it behind the 6th element ( that are seperated by / ) .
you have not shown any progress what you did! How about i gave you the hint?
assign a variable to your string, split it using \ and then find the exact occurrence to change by this [0] of course change the number according to your desire where change is required.
Now get your new string and concate with old in a way that you get desired results. Example
var str = "http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg";
var newstr = str.split('/')[5];
var prepstr = newstr + "something";
then finally,
str + prepstr;

Cut text using jquery

How to cut text using jQuery? For example :
if output like :
new/2016/songs1.mp3
new/2015/songsx.mp3
new/songs3.mp3
Need output :
songs1.mp3
songsx.mp3
songs3.mp3
I want to put only file name with extension like songs-name.mp3 not directory, so i want to cut this using jQuery.
split it and take the last item
var str = "new/2016/songs1.mp3";
var items = str.split( "/" );
alert(items[items.length - 1 ]);
or simply
alert( str.split("/").pop() );
if you want to remove the rest of the text then
var str = "new/2016/songs1.mp3";
var items = str.split( "/" );
str = items[items.length - 1 ];
or
str = str.split("/").pop();
Check it out:
Here i use split function to split the string and then its return an array.
From that array we have to get the last portion i.e, Song name, So we have to get the length of that array.
After that we alert the array with the index of last portion.
var str = "new/2016/songs1.mp3";
var arr = str.split("/");
alert(arr[(arr.length) - 1]);
try this
var filepath = "new/2015/songsx.mp3";
console.log(filepath.slice(filepath.lastIndexOf("/")+1));
Using the slice method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) you can get a part of a string.
Using the lastIndexOf method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) returns the index of the last occurrence of the given string (or -1 if given string does not occur in the string).
So what we are doing is getting the slice if the file path starting with the first character after the las "/" in the filepath variable.

Extract url from javascript String

I would like to extract the following String :
http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
from this String :
https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
And before, extract, i would like to check if the global String contains more than one time "http" to be sure to extract the jpg only when needed.
How can i do that ?
Extract the data like this:
var myStr = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var splittedStr = myStr.split("-");
var extractedStr = splittedStr[3].slice(1);
To find out how many "http" is present in the string:
var count = (myStr.match(/http/g)).length;
Hopes it helps
var source = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var temp = source.replace("https","http").split("http");
var result = 'http'+temp[2];
use split()
var original = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg";
original = original.split('-/');
alert($(original)[original.length-1]);
your require URL shows in alert dialog
You can use regex :
str.match(/(http?:\/\/.*\.(?:png|jpg))/i)
FIDDLE

Categories

Resources