I have following INPUT out.
pieChart.js
stackedColumnChart.js
table.js
and i want OUTPUT like that(wanna remove .js from )
pieChart
stackedColumnChart
table
var array = ['pieChart.js', 'stackedColumnChart.js', 'table.js'];
var modifiedArray = array.map(function(el) {
return el.replace('.js', '');
});
console.log(modifiedArray);
If input is a multi-line string:
var input = "pieChart.js\n" +
"stackedColumnChart.js\n" +
"table.js";
var output = input.replace(/\.js$/mg, '');
If it's an array:
var input = ["pieChart.js","stackedColumnChart.js","table.js"];
var output = $.map(input, function(el){
return el.replace(/\.js$/, '');
});
You can loop through the strings and take substring of those strings.
In this case :
var array = ['pieChart.js', 'stackedColumnChart.js', 'table.js'];
for (item in array){
newItem = item.substr(0, item.length-3);
console.log(newItem);
}
You just substring the characters except the last 3
var dotJS = "pieChart.js";
var withoutJS = dotJS.substr(0,dotJS.length-3);
alert (withoutJS);
Now you have a string minus those last three characters.
(pffft... Wow, I'm late with my answer here.)
Related
I have a string like "home/back/step" new string must be like "home/back".
In other words, I have to remove the last word with '/'. Initial string always has a different length, but the format is the same "word1/word2/word3/word4/word5...."
var x = "home/back/step";
var splitted = x.split("/");
splitted.pop();
var str = splitted.join("/");
console.log(str);
Take the string and split using ("/"), then remove the last element of array and re-join with ("/")
Use substr and remove everything after the last /
let str = "home/back/step";
let result = str.substr(0, str.lastIndexOf("/"));
console.log(result);
You could use arrays to remove the last word
const text = 'home/back/step';
const removeLastWord = s =>{
let a = s.split('/');
a.pop();
return a.join('/');
}
console.log(removeLastWord(text));
Seems I got a solution
var s = "your/string/fft";
var withoutLastChunk = s.slice(0, s.lastIndexOf("/"));
console.log(withoutLastChunk)
You can turn a string in javascript into an array of values using the split() function. (pass it the value you want to split on)
var inputString = 'home/back/step'
var arrayOfValues = inputString.split('/');
Once you have an array, you can remove the final value using pop()
arrayOfValues.pop()
You can convert an array back to a string with the join function (pass it the character to place in between your values)
return arrayOfValues.join('/')
The final function would look like:
function cutString(inputString) {
var arrayOfValues = inputString.split('/')
arrayOfValues.pop()
return arrayOfValues.join('/')
}
console.log(cutString('home/back/step'))
You can split the string on the '/', remove the last element with pop() and then join again the elements with '/'.
Something like:
str.split('/');
str.pop();
str.join('/');
Where str is the variable with your text.
I would like to know how can I remove the First word in the string using JavaScript?
For example, the string is "mod1"
I want to remove mod..I need to display 1
var $checked = $('.dd-list').find('.ModuleUserViews:checked');
var modulesIDS = [];
$checked.each(function (index) { modulesIDS.push($(this).attr("id")); })
You can just use the substring method. The following will give the last character of the string.
var id = "mod1"
var result = id.substring(id.length - 1, id.length);
console.log(result)
Try this.
var arr = ["mod1"];
var replaced= $.map( arr, function( a ) {
return a.replace("mod", "");
});
console.log(replaced);
If you want to remove all letters and keep only the numbers in the string, you can use a regex match.
var str = "mod125lol";
var nums = str.match(/\d/g).join('');
console.log(nums);
// "125"
If you don't want to split the string (faster, less memory consumed), you can use indexOf() with substr():
var id = "mod1"
var result = id.substr(id.indexOf(" ") -0);
console.log(result)
I would like to split the below value:
var stringValue = "DEENFR";
As we see, there are 3 anchors hence I have tried
1. var result = stringValue.split("<a/>");
2. var myRegexp = new RegExp("<a/>");
var result = stringValue.split(myRegexp);
What would be the best regex or way to get these 3 anchors ?
here's a pure regex solution:
var stringValue = 'DEENFR';
var ar = stringValue.match(/<a.*?<\/a>/g)
console.log(ar)
You can use positive look ahead assertion to get the splitting position
var stringValue = 'DEENFR';
var res = stringValue.split(/(?=<a.*?<\/a>)/);
console.log(res);
Regex explanation
You are already there, just one extra step (okay, maybe 2) is required
var finalArray = stringValue.split("</a>").filter( function(val){
return val.length > 0; //filter out empty results
}).map( function(val){
return val + "</a>"; //append "</a>" to the rest of the results
})
First your string contains double quotes (") you should either escape them or encapsulate it in single quotee (').
var stringValue = 'DEENFR';
var result = stringValue.split("</a>")
.filter(function(item) {
return item ? true : false;
})
.map(function(item) {
return item + '</a>';
});
So i have this object of photos, which is value of some hidden input:
53bd570ba13ef.jpg,53bd570c964c3.jpg,53bd570d311c9.jpg,53bd570db8997.jpg.
What i need is to remove last string witch number and the comma before: ,53bd570db8997.jpg.
var dataInput = $('#images'),
imgs = dataInput.val(),
thumbIndex = $(this).parent().index();
//
var _result = imgs.split(',')[thumbIndex];
//
var name = _result.slice(0, _result.indexOf(","));
console.log(name);
The thumbIndex is my photo number/name without the comma: 53bd570db8997.jpg. Can anybody help?
If I understood you right, I'd regexp it:
imgs.replace(new RegExp("," + thumbIndex),"");
imgs should be the string you posted above (the comma-separated one).
If you're sure that thumbIndex contains the last filename, you can get away with this:
var data = '53bd570ba13ef.jpg,53bd570c964c3.jpg,53bd570d311c9.jpg,53bd570db8997.jpg'; // or $('#images').val()
var thumbIndex = '53bd570db8997.jpg';
var result = data.substr(0, data.indexOf(thumbIndex) - 1);
Perhaps you could elaborate a little bit more on what precisely you want to achieve, but I'm going to make an attempt at understanding your question and I will try to give you a solution.
As I understand, you wish to get the last element from a string of values which are delimited by a ',' character.
You could of course split the string and simply get the last element from the array.
var dataInput = $('#images');
var imgs = dataInput.val();
var _result = imgs.split(',');
var thumbnail = _result[_result.length - 1];
console.log(thumbnail);
Here's a JSFiddle to try out: http://jsfiddle.net/WBb5F/1/
If I got you right, you can try lastIndexOf()
var result = data.substr(0, data.indexOf(','));
Fiddle
Using this html
<input type="hidden" value="53bd570ba13ef.jpg,53bd570c964c3.jpg,53bd570d311c9.jpg,53bd570db8997.jpg" id="images" />
To get the last item you can do this:
var dataInput = $('#images'),
imgs = dataInput.val(),
thumbIndex = imgs.split(',').length;
var name = imgs.split(',')[thumbIndex - 1]
console.log(','+ name);
Here is the FIDDLE
I want to extract the date and the username from string using .split() in this particular string:
var str ='XxSPMxX on 08/30/2012';
I want XxSPMxX in one variable and 08/30/2012 in the other.
Using just split:
var x = str.split('</a> on ');
var name = x[0].split('>')[1];
var date = x[1];
Demo: http://jsfiddle.net/Guffa/YUaAT/
I don't think split is the right tool for this job. Try this regex:
var str ='XxSPMxX on 08/30/2012',
name = str.match(/[^><]+(?=<)/)[0],
date = str.match(/\d{2}\/\d{2}\/\d{4}/)[0];
Here's the fiddle: http://jsfiddle.net/5ve7Y/
Another way would be to match using a regular expression, build up a small array to get the parts of the anchor, and then use substring to grab the date.
var str = 'XxSPMxX on 08/30/2012';
var matches = [];
str.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function () {
matches.push(Array.prototype.slice.call(arguments, 1, 4))
});
var anchorText = matches[0][2];
var theDate = str.substring(str.length - 10, str.length);
console.log(anchorText, theDate);
working example here: http://jsfiddle.net/dkA6D/