Problem getting substring in jquery - javascript

I have a string variable having data
4096;jpg,bmp
or
2048;flv,mpg
I want to split this string into two string in jquery on the basis of ;

You can use JavaScript's split function.
var some_text = "blah;blah;blahness";
var arr = some_text.split(';');
alert(arr[2]); //blahness

here is some code..
var str = '4096;jpg,bmp';
var elements = str.split(';');
alert( elements[0] ); // 4096
alert( elements[1] ); // jpg,bmp

Related

How to convert string to array of object?

I have the following string:
"[['ABB','ACC','ADD'],['FGG','FHH','FJJJ'],['MNN','MOO','MPP']]"
and I want to convert it to array of object
[['ABB','ACC','ADD'],['FGG','FHH','FJJJ'],['MNN','MOO','MPP']]
I've tried to do many things but I could not
function nextQuess() {
var ffa = JSON.stringify("<%- hola %>"); // from ejs variable "[['ABB','ACC','ADD'],['FGG','FHH','FJJJ'],['MNN','MOO','MPP']]"
// var ff = JSON.parse([ffa])
// console.log('hello', ff);
console.log("Hello", ffa);
}
You need to replace ' by " and then parse
'(.*?)'(?=(,|\])
'(.*?)' - Match ' followed by anything zero more time ( Lazy mode ) ( Capture group 1)
(?=(,|\])) - Match must be followed by , or ]
let str = "[['ABB','ACC','ADD'],['FGG','FHH','FJJJ'],['MNN','MOO','MPP']]"
let replacedString = str.replace(/'(.*?)'(?=(,|\]))/g, "\"$1\"")
let final = JSON.parse(replacedString)
console.log(final)
Use JSON.stringify(json) and then JSON.parse()
let jsonString = JSON.stringify([['ABB','ACC','ADD'],['FGG','FHH','FJJJ'],['MNN','MOO','MPP']]);
let array = JSON.parse(jsonString);
console.log(array);
Or you can also try eval() method
let jsonArray = eval([['ABB','ACC','ADD'],['FGG','FHH','FJJJ'],['MNN','MOO','MPP']]);
console.log(jsonArray);

how to remove array String substring() Method using javascript?

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)

Getting the last substring value of a string with hyphens in it

I need your help,
How can I get the last value (sub-string) of a text string that has hyphens in it?
var instr = "OTHER-REQUEST-ALPHA"
...some processing here to get the outstr value
var outstr = "ALPHA"
Use String#split and Array#pop methods.
var instr = "OTHER-REQUEST-ALPHA";
console.log(
instr.split('-').pop()
)
Or use String#lastIndexOf and String#substr methods
var instr = "OTHER-REQUEST-ALPHA";
console.log(
instr.substr(instr.lastIndexOf('-') + 1)
)
Or using String#match method.
var instr = "OTHER-REQUEST-ALPHA";
console.log(
instr.match(/[^-]*$/)[0]
)
The simplest approach is to simply split the string by your delimeter (ie. -) and get the last segment:
var inString = "OTHER-REQUEST-ALPHA"
var outString = inString.split("-").slice(-1)[0]
That's it!
Use SPLIT and POP
"String - MONKEY".split('-').pop().trim(); // "MONKEY"
Or This
string2 = str.substring(str.lastIndexOf("-"))

How to truncate certain text in javascript

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.)

How do I split this string with JavaScript?

Javascript:
var string = '(37.961523, -79.40918)';
//remove brackets: replace or regex? + remove whitespaces
array = string.split(',');
var split_1 = array[0];
var split_2 = array[1];
Output:
var split_1 = '37.961523';
var split_2 = '-79.40918';
Should I just use string.replace('(', '').replace(')', '').replace(/\s/g, ''); or RegEx?
Use
string.slice(1, -1).split(", ");
You can use a regex to extract both numbers at once.
var string = '(37.961523, -79.40918)';
var matches = string.match(/-?\d*\.\d*/g);
You would probably like to use regular expressions in a case like this:
str.match(/-?\d+(\.\d+)?/g); // [ '37.961523', '-79.40918' ]
EDIT Fixed to address issue pointed out in comment below
Here is another approach:
If the () were [] you would have valid JSON. So what you could do is either change the code that is generating the coordinates to produce [] instead of (), or replace them with:
str = str.replace('(', '[').replace(')', ']')
Then you can use JSON.parse (also available as external library) to create an array containing these coordinates, already parsed as numbers:
var coordinates = JSON.parse(str);

Categories