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);
Related
var cartstring = "27,00 - R"
How can I remove spaces and "-" and "R" using only regex (not allowed to use slice etc.)? I need to make strings cartstring1 and cartstring2 which should both be equal to "27,00", first by removing spaces and "-" and "R", and second by allowing only numbers and ",".
cartstring1 = cartstring.replace(/\s/g, "");
cartstring2 = cartstring.replace(/\D/g, "");
Please help me modify these regular expressions to have a working code. I tried to read about regex but still cannot quite get it. Thank you very much in advance.
you can just capture just what you are interested in number and comma:
let re = /[\d,]+/g
let result = "27,00 - R".match(re)
console.log(result)
You can group the characters you want to remove:
var cartstring = "27,00 - R"
let res = cartstring.replace(/(\s|-|R)/g, "")
console.log(res)
Or alternatively, split the string by a space and get the first item:
var cartstring = "27,00 - R"
let res = cartstring.split(" ")[0]
console.log(res)
You are using 2 replacements, one replacing all whitespace chars \s and the other replacing all non digits \D, but note that \D also matches \s so you could omit the first call.
Using \D will also remove the comma that you want to keep, so you can match all chars except digits or a comma using [^\d,]+ in a single replacement instead:
var cartstring = "27,00 - R";
console.log(cartstring.replace(/[^\d,]+/g, ''));
I want to replace the underscore that precedes a date in a string e.g.
thequick_brown_20210813_fox
To:
thequick_brown_red_20210813_fox
I.e. replace that underscore with _red_
This captures the date part: (20\d{2})(\d{2})(\d{2})
And to replace I assume I can just use str.replace
But not sure how I can capture the underscore that precedes it.
You can try the following RegEx with Positive Lookahead:
/_(?=\d{8})/
Where:
_ matches the character _
(?=\d{8}) - Positive Lookahead
\d - matches a digit (equivalent to [0-9])
{8} - matches the previous token exactly 8 times
var str = 'thequick_brown_20210813_fox';
var patt = /_(?=\d{8})/;
str = str.replace(patt, '_red_');
console.log(str);
This works
And as bonus I have a super robust regex for dates
const str = 'thequick_brown_20210813_fox';
const re = /_(?=\d{4}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01]))/;
const newStr = str.replace(re, '_red_');
console.log(newStr);
The regex is from here
This is what I came up with using the RegEx you provided.
let str = "thequick_brown_20210813_fox";
let newStr = "";
let regex = /(20\d{2})(\d{2})(\d{2})/;
let splits = str.split(regex);
newStr += splits[0] + "red_";
for(let i=1; i<splits.length; i++){
newStr += splits[i];
}
console.log(newStr);
I would use as a matching regex:
_(?=20[0-9]{6})
_ Matches underscore if ...
(?=20[0-9]{6}) This is a lookahead assertion that the following characters must be '20' followed by 6 digits.
Note that the above only matches the underscore when followed by '20' followed by an additional 6 digits. So we simply replace the underscore with '_red_':
let s = 'thequick_brown_20210813_fox';
s = s.replace(/_(?=20[0-9]{6})/g, '_red_');
console.log(s);
I need help in javascript where the word entered can be replaced as:
Input - A.. BC
Output - A-BC
the code that i have tried is:
var text = 'A.. BC';
new_text = text.replace(' ', '-') && text.replace('.','-');
console.log(new_text);
This is not working as it is giving me the output as:
A-. BC
I'd use a regular expression instead. Use a character set to match one or more dots, commas, or spaces, then replace with a dash:
const change = str => str.replace(/[., ]+/g, '-');
console.log(change('A.. BC'));
Use a charater set
var text = 'A.. BC';
new_text = text.replace(/[., ]+/g, '-');
console.log(new_text);
you can try replacing all non-alphabetical characters with a hyphen with regex:
const a = 'A.. BC';
const b = 'A ..BC';
// Find all non-word characters regex
const r = /[\W]+/g;
const hyphen = '-';
void console.log(a.replace(r, hyphen));
void console.log(b.replace(r, hyphen));
// A-BC
// A-BC
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
My goal is :
Delete all.
except the numbers , but delete the zeros who before numbers 1 to 9
And I have this regex:
var validValue = inputValue.replace(/[^\d]/g, '').replace(/^0*/g, '');
But I want to make it in a one replace()
So how can I do that ?
You want to remove all leading zeros and all non-digit symbols. It can be done with
/^0+|\D+/g
See the regex demo
The regex matches
^0+ - 1 or more leading digits (those at the beginning of the string)
| - or
\D+ - one or more non-digit symbols
var re = /^0*|\D+/g;
var str = '00567600ffg5566';
var result = str.replace(re, '');
document.body.innerHTML = str + " >>> " + result;