JavaScript how to strip/remove a char from string [duplicate] - javascript

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.

To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'

Related

How can I replace or delete a character from a string in React JS like normal Javascript? [duplicate]

This question already has answers here:
Replace all occurances in javascript
(4 answers)
Closed 2 years ago.
I have a string that contains multiple '-'. I want to remove the all '-'. I tried the following way but it didn't work.
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984"
var new_str = str.replace('-', '')
How can I remove or replace all the '-'? Is there any simple function for that?
Remove globally and remove the second parenthesis
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984";
var new_str = str.replace(/-/g, '');
console.log(new_str);

Modifying a string with letters, parentheses and numbers [duplicate]

This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 4 years ago.
How can I modify this string:
"SRID=4326;POINT (-21.93038619999993 64.1444948)"
so it will return
"-21.93038619999993 64.1444948"
(and then I can split that)?
The numbers in the string can be different.
I've tried using .replace & split, but I couldn't get it to work properly. How can I make this happen using Javascript?
You can try with match and regex:
"SRID=4326;POINT (-21.93038619999993 64.1444948)".match(/\(([^)]+)\)/)[1]
// "-21.93038619999993 64.1444948"
I am not good using REGEXP but this could be a solution with pure split.
Hope it helps :>
var str = "SRID=4326;POINT (-21.93038619999993 64.1444948)" ;
var newStr = str.split('(')[1].split(')')[0];
console.log(newStr)
var new_string = string.replace("SRID=4326;POINT (", "");
You can use a regular expression. The first number is put in first, the second number is put in second.
const INPUT = "SRID=4326;POINT (-21.93038619999993 64.1444948)";
const REGEX = /SRID=\d+;POINT \((.+) (.+)\)/
const [_, first, second] = INPUT.match(REGEX);
console.log(first);
console.log(second);

How to replace the backward slash from string? [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 4 years ago.
What is wrong with the following code?
Expected output : substr1#substr2#substr3
var str = "substr1\substr2\substr3"
// it works if I use the double slash "\\" in thestring but not with single.
console.log(str.replace(/\\/g, "#"));
Your initial string itself do not have a backslash. To verify check the snippet below:
var str = "substr1\sustr2\substr3"
console.log(str);
The actual output you expect can be obtain by first escaping the backslash and then replacing it with #:
var str = "substr1\\sustr2\\substr3"
console.log(str.replace(/\\/g, "#"));

Regex that detects greater than ">" and less than "<" in a string [duplicate]

This question already has answers here:
Regular expression greater than and less than
(3 answers)
Closed 8 years ago.
I need a regular expression that replaces the greater than and less than symbol on a string
i already tried
var regEx = "s/</</g;s/>/>/g"
var testString = "<test>"
alert(testString.replace(regEx,"*"))
My first time to use it please go easy on me :)
Thanks
You can use regEx | like
var regEx = /<|>/g;
var testString = "<test>"
alert(testString.replace(regEx,"*"))
Fiddle
For greater than and less than symbol.
var string = '<><>';
string = string.replace(/[\<\>]/g,'*');
alert(string);
For special characters
var string = '<><>';
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_');
alert(string);
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source :
http://www.dotnetperls.com/regex-replace

How to extract numbers from string in Javascript using regex [duplicate]

This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]

Categories

Resources