remove element empty in string Javascript [duplicate] - javascript

This question already has answers here:
Remove empty elements from an array in Javascript
(49 answers)
Closed 25 days ago.
var str = '1,2,1,,3';
str = Array.from(new Set(str.split(','))).toString(); // remove duplicates
// str = '1,2,,3'
console.log(str)
I need to remove elements empty in string.
e.g: '1,2,' => '1,2'
',1,2' => '1,2'
'1,,2' => '1,2'
Hope everybody help me.

var str = ',1,2,1,,,3,';
var removed = str.split(',').filter(c => c);
console.log(removed.join(','))

Related

How to remove + from string except at beginning using regex [duplicate]

This question already has answers here:
Replace all occurrences of character except in the beginning of string (Regex)
(3 answers)
Remove all occurrences of a character except the first one in string with javascript
(1 answer)
Closed 1 year ago.
I’m looking to remove + from a string except at the initial position using javascript replace and regex.
Input: +123+45+
Expected result: +12345
Please help
const string = '+123+45+'
console.log(string.replace(/([^^])\+/g, '$1'))
https://regexr.com/61g3c
You can try the below regex java script. Replace all plus sign except first occurrence.
const givenString = '+123+45+'
let index = 0
let result = givenString.replace(/\+/g, (item) => (!index++ ? item : ""));
console.log(result)
input = '+123+45+';
regex = new RegExp('(?!^)(\\+)', 'g');
output = input.replace(regex, '');
console.log(output);

Javascript - split values inside parentheses [duplicate]

This question already has answers here:
How to get function parameter names/values dynamically?
(34 answers)
Closed 3 years ago.
I am trying to split the following string:
delete(value1,value2);
I want to get the values and save them in a array:
var values = [value1,value2]
A regex would do the trick:
/\((.*)\)/g.exec('delete(value1,value2);')[1].split(',')
This captures anything between parentheses, which you can then split again.
You can split the string using split function
var str = "delete(value1,value2)";
var stringArray = str.split("(");
var values=stringArray[1].split(",");
use Replace method to finally delte ")" from second string in array
values[1]=values[1].replace(")","");
How about:
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
function getParamNames(fnStr) {
var fnStr = fnStr.replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = [];
return result;
}
var str = "delete(value1,value2)";
getParamNames(str);

Regex Javascript remove char after 'something' [duplicate]

This question already has answers here:
Remove string after predefined string
(4 answers)
Closed 4 years ago.
I had a sentence like this 'someurl.com/?something=1,2,3' i want to check if the char had something= then remove all character after that.
so like this.
'soome.url/?something=1,2,3,1' => 'soome.url/?'
'soome.url/nothing?nothingtoo?something=1,2,3,1' => 'soome.url/nothing?nothingtoo?'
'soome.url/nothing?something=1,2,3,1' => 'soome.url/nothing?'
how to do that in Javascript?
There are already other pretty good answers with split method.
If you still want to know how to do it with regex
let arr = [`soome.url/?something=1,2,3,1'`
,`soome.url/nothing?nothingtoo?something=1,2,3,1`,
`soome.url/nothing?something=1,2,3,1`]
arr.forEach(e=>{
console.log(e.replace(/\?(?:something=.*)$/g,'?'))
})
Using split and getting the first index will return the string before something=.
const urlOne = 'soome.url/?something=1,2,3,1' // 'soome.url/?'
const urlTwo = 'soome.url/nothing?nothingtoo?something=1,2,3,1' // 'soome.url/nothing?nothingtoo?'
const urlThree = 'soome.url/nothing?something=1,2,3,1' // 'soome.url/nothing?'
function strip(url){
return url.split('something=')[0];
}
console.log(strip(urlOne));
console.log(strip(urlTwo));
console.log(strip(urlThree));
Assuming it's always the last parameter, you could just split the url at something:
const url = 'soome.url/nothing?nothingtoo?something=1,2,3,1';
const newUrl = url.split("something")[0]
console.log(newUrl)
You can also try like following using substr.
let url1 = "soome.url/nothing?nothingtoo?something=1,2,3,1";
let pattern = "something=";
let str2 = url1.substr(0, url1.indexOf(pattern) <= 0 ? str1.length : url1.indexOf(pattern));
console.log(str2);

pass variable to replace function regular expression javascript [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 6 years ago.
I want to have a 'newline'-function to pass a string to it and print it on pdf. my function so far is
var array = new Array();
function newLineFunction_PDF(text) {
var arr = text.replace(/.{70}\S*\s+/g, "$&#").split(/\s+#/);
return arr;
}
array = newLineFunction_PDF('some Text');
for( var i in array) {
print(array[i]);
}
What it does is cut the text in to pieces of length-70 incl. the last word, push it into the array and print it afterwards with new lines. Now i want to pass a number to the function, like 100, so i can decide the max-length of the text per line.
So far I tried:
function newLineFunction_PDF(text, num) {
var re = new RegExp(/.{num}\S*\s+/g);
var arr = text.replace(re, "$&#").split(/\s+#/);
return arr;
}
but I dont know how and where to add escapes into the new RegExp.
The parameter of Regexp is a string:
var re = new RegExp('.{' + num + '}\S*\s+', 'g');

How to trim a string to its last four characters? [duplicate]

This question already has answers here:
How to get the last character of a string?
(15 answers)
Closed 8 years ago.
I know there are methods to remove characters from the beginning and from the end of a string in Javascript. What I need is trim a string in such a way that only the last 4 characters remain.
For eg:
ELEPHANT -> HANT
1234567 -> 4567
String.prototype.slice will work
var str = "ELEPHANT";
console.log(str.slice(-4));
//=> HANT
For, numbers, you will have to convert to strings first
var str = (1234567).toString();
console.log(str.slice(-4));
//=> 4567
FYI .slice returns a new string, so if you want to update the value of str, you would have to
str = str.slice(-4);
Use substr method of javascript:
var str="Elephant";
var n=str.substr(-4);
alert(n);
You can use slice to do this
string.slice(start,end)
lets assume you use jQuery + javascript as well.
Lets have a label in the HTML page with id="lblTest".
<script type="text/javascript">
function myFunc() {
val lblTest = $("[id*=lblTest]");
if (lblTest) {
var str = lblTest.text();
// this is your needed functionality
alert(str.substring(str.length-4, str.length));
} else {
alert('does not exist');
}
}
</script>
Edit: so the core part is -
var str = "myString";
var output = str.substring(str.length-4, str.length);

Categories