Javascript regex replace function - javascript

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');

Related

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, ''));

Replace string with substring with regular expression

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

How do I replace all occurrences of “</p>” in a string with “” in JavaScript?

I get errors when I try any of the following:
str = str.replace(/</p>/gm, "")
str = str.replace("</p>"/gm, "")
What am I doing wrong?
The '/' is a special character that you have to escape it within the regex:
str = str.replace(/<\/p>/gm, "");
tr = str.replace(/\<\/p\>/gi, "")
You have to escape the backslash character.
str = str.replace(/<\/p>/gm, "");
See example here.

Replace comma by double quote in javascript

How to replace comma by double quote in javascript?
For example: "c,C#,JavaScript" should be like "C","C#","JavaScript"
str = '"c,C#,JavaScript"';
str = str.split(',').join('","');
That would result in "c","C#","JavaScript"
var original = '"c,C#,JavaScript"';
var quoted = original.replace(/,/g, '","'); // "c","C#","JavaScript"
Just to toss it in there, you could also .split() and .join(), like this:
var oldString = '"c,C#,JavaScript"';
var newString = oldString.split(',').join('","');
You can test it here.
You can do this:
str = "c,C#,JavaScript";
str = str.replace(/,/g, '"');
Result:
c"C#"JavaScript

Categories

Resources