Replace string with substring with regular expression - javascript

If I have a string like this 100,000 some digits with , followed by some other digits. I need to replace all digits so that the string become 100.
I have to use a 'replace()' function so please provide the expression.
var str = "1200,00";
str.replace("Expression");// Need this.

var str = "1200,00".split(",")[0];
alert(str);
using .replace it will replace all characters with '' after the comma.
var str = "1200,00".replace(/,.*/, '');
alert(str);

Use this Regular Expression:
var str = "1200,00";
str.replace(/,\d+/,'');
I tested and it works.
Hope that helps.

<script type="text/javascript">
var myValue = replace("1200,00");
function replace(str)
{
var retValue = str.replace(/,\d+/,'');
return retValue;
}
</script>
you can use a function for generic method

Related

How to replace a string with square brackets using javascript replace function?

I have a string [TEST][NO CHANGE][TEST][NOW][TEST] in which [TEST] should be replace with 'replaced', and the result should be replaced[NO CHANGE]replaced[NOW]replaced.
I have Tried the following ways, nothing worked.
1. str.replace(/'[TEST]'/g, 'replaced');
2. str.replace(/[TEST]/g, 'replaced');
3. str.replace('/[TEST]/g', 'replaced');
var str = "[TEST][NO CHANGE][TEST][NOW][TEST]";
var resultStr = str.replace(/'[TEST]'/g, 'replaced');
Actual String: [TEST][NO CHANGE][TEST][NOW][TEST]
After Replacing: replaced[NO CHANGE]replaced[NOW]replaced
[] has a special meaning in regex, which means character class, if you want to match [] you need to escape them
var str = "[TEST][NO CHANGE][TEST][NOW][TEST]";
var resultStr = str.replace(/\[TEST\]/g, 'replaced');
console.log(resultStr)
Try to update using Below snippet.
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
var str = "[TEST][NO CHANGE][TEST][NOW][TEST]";
var result = str.replaceAll('\[TEST\]','replaced')
console.log(result);
replaced[NO CHANGE]replaced[NOW]replaced
Src : How to replace all occurrences of a string in JavaScript
Your regular expression in replace is looking for the string '[TEST]' surrounded by those single quotes and is looking to match any of the characters in TEST because you didn't escape the brackets. Try this regular expression instead:
var resultStr = str.replace(/\[TEST\]/g, 'replaced');

remove second comma from a string in javascript

I have an string as:
0123456789,, 0987654213 ,, 0987334213,, ......
How can I convert this into
0123456789, 0987654213, 0987334213, ......
i.e I want to remove the second comma
You can do it very simply, like this using regex.
var str = "0123456789,,0987654213,,0987334213,,,,,9874578";
str=str.replace(/,*,/g,',');
console.log(str)
var str = "0123456789,, 0987654213 ,, 0987334213,, ......"
console.log(str.replace(/\,+/g,","));
This will replace all occurrences of consecutive commas with a single comma:
str.replace(/,+/g, ',');
You can use replace() method with regular expression with g flag to replace all instances ',,' with ',':
str.replace(/,,/g, ",");
Here's a simple example
var str = '0123456789,, 0987654213 ,, 0987334213';
str = str.replace(/,,/g, ",");
console.log(str);
var str = "0123456789,, 0987654213 ,, 0987334213,,"
str = str.split(",,").join(",")
console.log(str);
There is a replace method for String.
You can replace ',,' with a ','.
An example:
var str = "0123456789,, 0987654213,, 0987334213,";
var newStr = str.replace(/,,/g,','));
The output:
0123456789, 0987654213, 0987334213,

Replace expression between two punctuation letters in javascript

I have a string like the following,
var str = "abcd-12ad3dgs4g56.com"
I want like the following from this
abcd.com
I have to replace only -*. expression with ..
How do I do this in JavaScript?
Simply try this
str.replace( /-\w+/, "" ); //"abcd.com"
var str = "abcd-12ad3dgs4g56.com"
console.log(str.replace(/-\w+/, ""));
You could use a regular expression with a positive lookahead.
var str = "abcd-12ad3dgs4g56.com";
console.log(str.replace(/-.*(?=\.)/g, ''));

String : Replace function with expression in Javascript

A string representing a currency is to be converted to a number.
For example:
Input : "125.632.454.454.403,51"
Output expected : 125632454454403.51
Currently I am trying:
Trial 1)
a = "125.632.454.454.403,51";
a.replace(/./, '');
Result = "25.632.454.454.403,51"
Trial 2)
a = "125.632.454.454.403,51";
a.replace(/./g, '');
Result = ""
But I expect the replace function to find all the occurrences of "." and replace by "".
Trial 3)
a = "125.632.454.454.403,51";
a.replace(/,/, '');
Result = "125.632.454.454.40351"
I would be glad if I find a fix for this.
You need to use \. instead of .. The dot (.) matches a single character, without caring what that character is. Also you can do it with single replace() with callback .
var str = "125.632.454.454.403,51";
str = str.replace(/\.|,/g, function(m) {
return m == '.' ? '' : '.'
});
document.write(str);
try:
var str = "125.632.454.454.403,51" ;
var result = str.replace(/\./g,'').replace(/\,/g,'.');
console.log(Number(result))
replace returns the changed string, it does not change it in-place!
You can find this out, by refering to the documentation.
Use
var Result = a.split('.').join("");
console.log(Result);
. has specific meaning in a regex. It matches any character. You need to escape the dot if you are actually looking for the character itself
var a = "125.632.454.454.403,51";
var result = a.replace(/\./g,"");
You can also do (parseFloat(a.replace(/[^0-9]+/g,""))/100)
And if you have to do this for multiple currencies, I would recommend looking into autonumeric.js. It handles all this for you.

Javascript regex replace function

If I have the following string:
var str = "Test.aspx?ID=11&clicked=false+5+3";
str = str.replace(????????, 'true');
How can I replace the substring "false+5+3" with "true" using REGEX?
Thanks in advance!!!
str = str.replace(/false\+5\+3/, 'true');
You need to escape the + since it means something special in regex.
str = str.replace(/clicked=[^&]*/, 'clicked=true');
this will replace anything in clicked parameter, not only false+...
var str = "Test.aspx?ID=11&clicked=false+5+3";
str = str.replace(/false[+]5[+]3/, 'true');

Categories

Resources