Like the title says, i would like to remove an underscore within a String with a regex. This is what i have:
function palindrome(str) {
str = str.toLowerCase().replace(/[^a-zA-Z]/g, '',/\s/g, '',/[0-9]/g,'');
if (str.split("").reverse().join("") !== str) {
return false;
}
else {
return true;
}
}
palindrome("eye");
Use .replace(/_/g, "") to remove all underscores or use .replace(/_/g, " ") to replace them with a space.
Here is an example to remove them:
var str = "Yeah_so_many_underscores here";
var newStr = str.replace(/_/g, "");
alert(newStr);
You can use .replace to achieve this. Use the following code. It will replace all _ with the second parameter. In our case we don't need a second parameter so all _ will be removed.
<script>
var str = "some_sample_text_here.";
var newStr = str.replace(/_/g , "");
alert ('Text without underscores : ' + newStr);
</script>
str.replace(/_/g, '');
This should work.
you can remove underscore from response or any string
like this: "hello_rizo"
Code:
var rizo="hello_rizo"
console.log('output', e.response.data.message.replace(/(^|_)./g, s => s.slice(-1).toUpperCase()));
output:
hellorizo
Related
There is a problem.
After JSON.stringify I have a string:
value.value_new = {"apiunits":{"amount":"0"},"total":{"requests":"10","results":"10"},"project":{"projects":"1"}};
I want to replace { and }, and I try to do it this way:
value.value_new = value.value_new.replace("/[{}]/g", " ");
or
value.value_new = value.value_new.replace("/{/g", " ");
value.value_new = value.value_new.replace("/}/g", " ");
But it doesn't work. Why?
value.value_new is not a string, so you can't use the function replace on it.
If you just need this string without the brackets, you can use this method :
var value = {};
value.value_new = '{"apiunits":{"amount":"0"},"total":{"requests":"10","results":"10"},"project":{"projects":"1"}}';
String.prototype.replaceAll = function(str1, str2, ignore)
{
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
}
value.value_new = value.value_new.replaceAll("{", '');
value.value_new = value.value_new.replaceAll("}", '');
console.log(value.value_new); // You'll have the string that you want
I'm not sure why you would want to do that or what you would do with the result, but I think the RegEx you want is:
var asString = '{"apiunits":{"amount":"0"},"total":{"requests":"10","results":"10"},"project":{"projects":"1"}};'
//Replace either curly with a space. Note in RegEx you have to escape the curly braces with a backslash
var replaced = asString.replace(/\{|\}/gi, ' ');
console.log(replaced);
//outputs -->" "apiunits": "amount":"0" ,"total": "requests":"10","results":"10" ,"project": "projects":"1" ;"
In my example, I'm using the pipe for "or" but either of your previous attempts would work if the curly braces were escaped:
value = value.replace("/[\{\}]/g", " ");
or
value = value.replace("/\{/g", " ");
value = value.replace("/\}/g", " ");
If I have a input value "a[123],b[456],c[789]" and I want to return as "a=123&b=456&c789"
I've tried below code but no luck.. Is there a correct way to implement this?
var str = "a[123],b[456],c[789]"
var string = (str).split(/\[|,|\]/);
alert(string);
One option is:
var rep = { '[': '=', ']': '', ',': '&' };
var query = str.replace(/[[,\]]/g, el => rep[el] );
The delimiters are already there, it's just a matter of replacing one delimiter with another. Replace each [ with an =, replace each , with an &, and remove all ].
var str = "a[123],b[456],c[789]"
var string = str.replace(/([a-z])\[(\d+)],?/g, '$1=$2&').slice(0, -1);
alert(string);
Brute force way im not good at Regex. Just adding my thoughts
var str = "a[123],b[456],c[789]"
str = str.replace(/],/g, '&');
str = str.replace(/\[/g, '=');
str = str.replace(/]/g,'');
alert(str);
The simple 2 line answer for this is:
str=str.replace(/,/g,"&");
str=str.replace(/(\w)\[(\d+)\]/g,"$1=$2");
I want to remove the white space which is there in the start of the string
It should remove only the space at the start of the string, other spaces should be there.
var string=' This is test';
This is what you want:
function ltrim(str) {
if(!str) return str;
return str.replace(/^\s+/g, '');
}
Also for ordinary trim in IE8+:
function trimStr(str) {
if(!str) return str;
return str.replace(/^\s+|\s+$/g, '');
}
And for trimming the right side:
function rtrim(str) {
if(!str) return str;
return str.replace(/\s+$/g, '');
}
Or as polyfill:
// for IE8
if (!String.prototype.trim)
{
String.prototype.trim = function ()
{
// return this.replace(/^\s+|\s+$/g, '');
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
if (!String.prototype.trimStart)
{
String.prototype.trimStart = function ()
{
// return this.replace(/^\s+/g, '');
return this.replace(/^[\s\uFEFF\xA0]+/g, '');
};
}
if (!String.prototype.trimEnd)
{
String.prototype.trimEnd = function ()
{
// return this.replace(/\s+$/g, '');
return this.replace(/[\s\uFEFF\xA0]+$/g, '');
};
}
Note:
\s: includes spaces, tabs \t, newlines \n and few other rare characters, such as \v, \f and \r.
\uFEFF: Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF)
\xA0: ASCII 0xA0 (160: non-breaking space) is not recognised as a space character
Try to use javascript's trim() function, Basically it will remove the leading and trailing spaces from a string.
var string=' This is test';
string = string.trim();
DEMO
So as per the conversation happened in the comment area, in order to attain the backward browser compatibility just use jquery's $.trim(str)
var string=' This is test';
string = $.trim(string)
You can use String.prototype.trimStart(). Like this:
myString=myString.trimStart();
An if you want to trim the tail, you can use:
myString=myString.trimEnd();
Notice, this 2 functions are not supported on IE. For IE you need to use polyfills for them.
You should use javascript trim function
var str = " Hello World! ";
alert(str.trim());
This function can also remove white spaces from the end of the string.
Easiest solution: (ES10 feature trimStart())
var string= 'This is a test';
console.log(string.trimStart());
.trimLeft() can be used for this.
const str = " string ";
console.log(str.trimLeft()); // => "string "
Just a different and a not so efficient way to do it
var str = " My string";
function trim() {
var stringStarted = false;
var newString = "";
for (var i in str) {
if (!stringStarted && str[i] == " ") {
continue;
}
else if (!stringStarted) {
stringStarted = true;
}
newString += str[i];
}
return newString;
}
console.log(trim(str));
I am really sure this doesn't work for anything else and is not the most optimum solution, but this just popped into my mind
You want to remove the space (i.e whitespace) at the beginning of the string. So, could not use standard jquesy .trim(). You can use the regex to find and replace the whitespace at the beginning of the string.
Try this:
.replace(/^\s+/g, "")
Read this post
Try this:
var string=' This is test ';
alert(string.replace(/^\s+/g, ""));
Working Example
OR if you want to remove the whitespace from the beginning and end then use .trim()
var string=' This is test';
$.trim(string);
I want to remove the characters [ and ] inside a variable. How can I do this? My variable is something similar to this one:
var str = "[this][is][a][string]";
Any suggestion is very much appreciated!
Behold the power of regular expressions:
str = str.replace(/[\]\[]/g,'');
the fastest way
function replaceAll(string, token, newtoken) {
while (string.indexOf(token) != -1) {
string = string.replace(token, newtoken);
}
return string;
}
All you need to do is this...
var str = "[this][is][a][string]";
srt=replaceAll(str,'[',''); //remove "["
str=replaceAll(str,']',''); //remove "]"
how can i trim the word in Javascript.....???
If you can use jQuery, simply use $.trim().
If not, just add a trim method to the String prototype if the browser doesn't support it natively:
if(!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '')
}
}
Then you can use var newstring = somestring.trim();
If by trim, you mean remove extra whitepace, put this at the top of your script
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
And use it like this
var thestring = " hello ";
alert(thestring.trim());
You can add the function below
// Trim function in javascript
function Trim(str)
{
while (str.substring(0,1) == ' ') // check for white spaces from beginning
{
str = str.substring(1, str.length);
}
while (str.substring(str.length-1, str.length) == ' ') // check white space from end
{
str = str.substring(0,str.length-1);
}
return str;
}
Hope it helped...