how to remove First text from string in jquery? - javascript

var selectedCustomers = document.getElementById("hfCustomerID");
output :- "|1|2|3"
this is my hidden field in which pushed value stored.
values in hidden field are stored "|1|2|3" like this
because of this when string is split and pushed in array at index 0 =
"" is stored.
how to check and remove First "|" from string in jquery before pushing it to array?

"|1|2|3".slice(1).split('|');
use slice() to remove the first character.
"|1|2|3".match(/[^|]/g);
or use Regx

You can use substr to remove the first char like this,
string.substr(1);
console.log("|1|2|3".substr(1).split('|'));

You can use substring to remove first character from string.
var trimmed = "|1|2|3".substring(1);
alert (trimmed)

This can be done in multiple ways.
If value is always expected like you mentioned then can use string.substr(1).split('|') as suggested in one of the answer, else you can do it using "filter".
selectedCustomers.split('|').filter(function(value) {
return value !== "" && value !== null;
});

console.log("|1|2|3".slice(1).split('|'));

The following way you can do this.
trimmed= "|1|2|3".substring(1).split('|');
alert(trimmed);

Please refer to the code below:
var selectedCustomers = document.getElementById("hfCustomerID").substring(1);

Related

Split does not separate with tokens

I have this string defined
const str : string = 'hostel:uk>london>city>street';
that I want to split to see only hostel, but I see the whole string in the console
console.log(str.split([":"][1]));
You could match the first part until colon.
const
str = 'hostel:uk>london>city>street',
first = str.match(/^[^:]+/)[0];
console.log(first);
You need to put the [] after the split() as the output of a split() is an array.
So your code will change to,
console.log(str.split([":"])[0]);
Also, after the split(), "hostel" will be at the 0th index of the array.
You need to select item 0, and also do that after the split call (not inside it), and you need to pass a string into split:
console.log(str.split(":")[0]);
you can perform your desired output after adding this line
console.log(str.split(':')[0]);

How to get value between 2 character

I have an Id of '2015-11-30_1112_3'. How do I get the values between the two underscores(_) so I am left with '1112'.
Please note that the length of the string varies.
simplest solution would be
var value = '2015-11-30_1112_3';
alert( value.split( "_" )[ 1 ] );
just split the variable, which should give you an array of 3 items. Second item is what you are looking for
You can of course use a regular expression:
s.match(/_(.*)_/)[1]
Explanation: I assume s is your string. The expression matches everything, i.e. (.*), between two underscores. You have to select index 1 of the result because index 0 will give you the complete match including the underscores. Subsequent elements contain the bracketed groups.
You can do this if you are sure that all ids have the same format:
var str= '2015-11-30_1112_3';
var array=str.split("_");
alert(array[1]);
Write like this..
var value = "2015-11-30_1112_3";
var value1 = value.match(/_(.+)_/g);
Demo : Click here
Please try below solution with the help of this solution you can find value between two different symbols as well.
var str = "2015-11-30_1112_3";
var newStr = str.split('_')[1].split('_')[0];
alert(newStr);

Javascript RegExp matching returning too many

I need to take a string and get some values from it. I have this string:
'tab/tab2/tab3'
The '/tab3' is optional so this string should also work:
'tab/tab2'
I currently am trying this which works for the most part:
'tab/tab2/tab3'.match(new RegExp('^tab/([%a-zA-Z0-9\-\_\s,]+)(/([%a-zA-Z0-9-_s,]+)?)$'));
This will return:
["tab/tab2/tab3", "tab2", "/tab3", "tab3"]
but I want it to return
["tab/tab2/tab3", "tab2", "tab3"]
So I need to get rid of the 3rd index item ("/tab3") and also get it to work with just the 'tab/tab2' string.
To complicate it even more, I only have control over the /([%a-zA-Z0-9-_s,]+)? part in the last grouping meaning it will always wrap in a grouping.
you don't need regex for this, just use split() method:
var str = 'tab/tab2/tab3';
var arr = str.split('/');
console.log(arr[0]); //tab
console.log(arr[1]); //tab2
jsfiddle
I used this regexp to do this:
'tab/tab2/tab3'.match(new RegExp('^tab/([%a-zA-Z0-9\-\_\s,]+)(?:/)([%a-zA-Z0-9-_s,]+)$'));
Now I get this return
["tab/tab2/tab3", "tab2", "tab3"]
Now I just need to allow 'tab/tab2' to be accepted aswell...
Do not put regex between " or ', using /g to make global search else only first occurrence is returned
"tab/tab2/tab3".match(/tab[0-9]/g)

How to remove comma from number which comes dynamically in .tpl file

i want to remove comma from a number (e.g change 1,125 to 1125 ) in a .tpl file.
The value comes dynamically like ${variableMap[key]}
var a='1,125';
a=a.replace(/\,/g,''); // 1125, but a string, so convert it to number
a=parseInt(a,10);
Hope it helps.
var a='1,125'
a=a.replace(/\,/g,'')
a=Number(a)
You can use the below function. This function can also handle larger numbers like 123,123,123.
function removeCommas(str) {
while (str.search(",") >= 0) {
str = (str + "").replace(',', '');
}
return str;
};
var s = '1,125';
s = s.split(',').join('');
Hope that helps.
✨ ES2021 ✨ added replaceAll, so no need for regular expression:
const str = '1,125,100.05';
const number = parseFloat(str.replaceAll(",", ""));
You can use Regular Expression to change as it is faster than split join
var s = '1,125';
s = s.replace(/,/g, '');
//output 1125
Incoming value may not always be a string. If the incoming value is a numeric the replace method won't be available and you'll get an error.
Suggest using isNaN to see if numeric, then assume string and do replacement otherwise.
if(isNaN(x)) {
x = parseInt(x.replace(/[,]/g,''));
}
(Not foolproof because 'not number' doesn't prove it is a string, but unless you're doing something very weird should be good enough).
You can also add other symbols to the character group to remove other stray chars (such as currency symbols).

how to check next existence of a character in string in javascript

Hi All I have a string like this
var data='mobile,car,soap,room';
I am parsing from this string and making this string as comma sperated and pushing it into an array like this
var availableTags=[];
var str='';
for(i=0;i<data.length;i++)
{
if(data[i]==',')
{
availableTags .push(str);
str='';
}
else
{
str +=data[i];
}
}
But I am doing wrong as I cannot get the last value after comma... Now What I want to ask how can I come to know the next existence of , in my string that whether it exists or not. So that I can also include the last value.
I would also appreciate if someone guide me that how can I accomplish that same task some other way.
I want the string to be an array and it should look like this
["mobile","car","soap","room"]
you can use
var availableTags = data.split(",");
it will handle all the things. and will result in an array.
You can use:
data.split(/,/)
See the split documentation.
After loop, you need to check value of str and add it too. It can contain the rest after last comma, or it can be empty in case comma was the last character in data.
But as other pointed, split is probably better way to do it.
As far as fixing your existing function, try adding the following after the for loop:
if (str != "")
availableTags.push(str);
(When the loop ends str holds whatever came after the last comma.)
But like the other answers said, you can just use the array .split() method:
var availableTags = data.split(",");
You could append an extra comma on at the start:
data = data + ","
...

Categories

Resources