I trying to remove some string from string which I have.
I have following string.
"[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
I want to remove text between ^ # including that character.
Output should be "[1fe3-46675-be1a-cd97084b] dsd dsds [4j34-46675-be1a-cd97854b]"
I used following but, not removing that string.
let str = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
str = str.replace(/^.*#/g, '');
console.log(str);
You can do it with this regex.
let stringsS = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
let regex = /\^(.*?)\#/gi
console.log(stringsS.replace(regex,''));
Try this:
str = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#";
replacedStr = str.replace(/\^[^#]*\#/g, '');
console.log(replacedStr)
Escape the ^ as in:
str = str.replace(/\^.*#/, '');
The ^ means something in regex and must be escaped if you want it to be treated as normal character
Related
I have the next problem. I need to remove a part of the string before the first dot in it. I've tried to use split function:
var str = "P001.M003.PO888393";
str = str.split(".").pop();
But the result of str is "PO888393".
I need to remove only the part before the first dot. I want next result: "M003.PO888393".
Someone knows how can I do this? Thanks!
One solution that I can come up with is finding the index of the first period and then extracting the rest of the string from that index+1 using the substring method.
let str = "P001.M003.PO888393";
str = str.substring(str.indexOf('.')+1);
console.log(str)
You can use split and splice function to remove the first entry and use join function to merge the other two strings again as follows:
str = str.split('.').splice(1).join('.');
Result is
M003.PO888393
var str = "P001.M003.PO888393";
str = str.split('.').splice(1).join('.');
console.log(str);
You could use a regular expression with .replace() to match everything from the start of your string up until the first dot ., and replace that with an empty string.
var str = "P001.M003.PO888393";
var res = str.replace(/^[^\.]*\./, '');
console.log(res);
Regex explanation:
^ Match the beginning of the string
[^\.]* match zero or more (*) characters that are not a . character.
\. match a . character
Using these combined matches the first characters in the string include the first ., and replaces it with an empty string ''.
calling replace on the string with regex /^\w+\./g will do it:
let re = /^\w+\./g
let result = "P001.M003.PO888393".replace(re,'')
console.log(result)
where:
\w is word character
+ means one or more times
\. literally .
many way to achieve that:
by using slice function:
let str = "P001.M003.PO888393";
str = str.slice(str.indexOf('.') + 1);
by using substring function
let str = "P001.M003.PO888393";
str = str.substring(str.indexOf('.') + 1);
by using substr function
let str = "P001.M003.PO888393";
str = str.substr(str.indexOf('.') + 1);
and ...
Below is my code.
var str = 'test//123_456';
var new_str = str .replace(/\//g, '').replace(/_/g, '');
console.log(new_str);
It will print test123456 on the screen.
My question is how to do it in same regular express? not replace string twice.
Thanks.
Use character class in the regex to match any character in the collection. Although use repetition (+, 1 or more) for replacing // in a single match.
var new_str = str .replace(/[/_]+/g, '');
var str = 'test//123_456';
var new_str = str.replace(/[/_]+/g, '');
console.log(new_str);
FYI : Inside the character class, there is no need to escape the forward slash(in case of Javascript RegExp).
Use the regex to match the list of character by using regex character class.
var str = "test//123_456";
var nstr = str.replace(/[\/_]/g, '');
How to Replace -84 in a string: my-name-is-dude-84 with '' Regex?
I means the last '-' + number
I tried :
string = 'my-name-is-dude-84';
let regex = /[^\-*][1-9]/;
let specialChar = string.replace(regex, '');
then I received is my-name-is-dude-
I expect my string will be: my-name-is-dude
You're close, but this is what you need to do (I guess)
string = 'my-name-is-dude-84';
let regex = /-\d+$/;
let specialChar = string.replace(regex, '');
document.write(specialChar);
Your [^\-*] tries to match all characters but \, - and *. Also [1-9] only matches one digit (between 1 and 9). Use \d (all digits), and add a + to make it match one or more. Also, adding an end of string anchor $ to it makes it only match the hyphen+number at the end of the string.
You can use this regex (.*?)-\d+$
regex demo
JavaScript demo
string = 'my-name-is-99-dude-84';
let regex = /(.*?)-\d+$/;
let specialChar = string.replace(regex, "$1");
document.write(specialChar);
The best way to explain this is by example. I'm using jQuery to do this.
Example I have a string
var str = "1.) Ben"
how can I dynamically omit the character 1.) including the space such that str === "Ben"
str can be dynamic such that order can increment from ones, tens, to hundreds.
E.G.
var str = "52.) Ken Bush"
or
var str = "182.) Hailey Quen"
Expected output
str === "Ken Bush"
or
str === "Hailey Quen"
Example
var str = "182.) Hailey Quen"
var test = str.split(') ');
test = test[1];
//output "Hailey Quen"
You can use regex replacement to get what you want.
var str = "182.) Hailey"
var newStr = str.replace(/^\d+\.\)\s*/, '')
// Hailey
var s = "1456.) Hard Spocker".replace(/^\d+\.\)\s*/, '')
// Hard Spocker
^ makes sure that the pattern is matched at the start of the string only
\d+ will match one or more digits.
\. will match the . with escaping
) is a symbol so we need to escape it using \ as \)
\s* will match one or more spaces.
You can learn about these symbols here.
Try using .substring() and .indexOf() as shown :-
var str = "182.) Hailey Quen"
alert(str.substring(str.indexOf(' ')))
DEMO
OR use .split() as shown :-
var str = "182.) Hailey Quen"
alert($.trim(str.split(')')[1]))
DEMO
You can do it regular expression,
var str = "52.) Ken".replace(/\d+\.\)\s/g,"");
console.log(str); //Ken
DEMO
If you have zero or more than zero spaces after the ) symbol then you can use *,
var str = "52.) Ken".replace(/\d+\.\)\s*/g,"");
console.log(str); //Ken
Dismantling regex used,
/ states regex left border
\d d states normal character d, if we want to make it match
numbers then we have to escape it with \
+ It states that one or more number should be there.
\. Again . is a metacharacter to match any valid character, so
escape it.
\) Parenthesis is also a metacharacter to close a group, escape
it.
\s* 12.) can be followed by zero or more spaces.
/ states regex right boundary.
g global flag, which used to do a search recursively.
You can do it like this
var testURL = "182.) Hailey Quen";
var output = testURL.substring(testURL.lastIndexOf(")") + 1).trim();
console.log(output);
*trim function will help to remove extra space if any.Hope it will help
In javascript, how do I remove all special characters from the string except the semi-colon?
sample string: ABC/D A.b.c.;Qwerty
should return: ABCDAbc;Qwerty
You can use a regex that removes anything that isn't an alpha character or a semicolon like this /[^A-Za-z;]/g.
const str = "ABC/D A.b.c.;Qwerty";
const result = str.replace(/[^A-Za-z;]/g, "");
console.log(result);
var str = "ABC/D A.b.c.;Qwerty";
var result = str.replace(/[^A-Za-z;]/g, ""); // 21ABCDAbc;Qwerty
Live DEMO