This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I have the following code:
var fileName = "C:\fakepath\a.jpg";
fileName = fileName.replace(/.*(\/|\\)/, '')
and this will return just the a.jpg as intended but I don't understand how it knew to replace the the characters between both of "\" such as the substring "fakepath". From what I see it should just replace the first character "C" because of the period and then any appearances of "/" or "\" with "".
The . means any character.
The * means zero or more.
So .* matches from the start of the string to the point where matching any more characters would prevent the rest of the regular expression from matching anything.
Related
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
I have this sorta string = "^My Name Is Robert.^"
I want to remove the occurrences of ^ from this string. I tried the replace method like :
replyText.replace(/^/g, '');
But it hasn't any affect. Using the replace without the global works but only removes the first occurrence.
Should I just make a loop and keep looping the string with replace till no more '^' are contained, or is there a better way?
You need to escape the ^ character in RegEx:
replyText.replace(/\^/g, '');
The caret, ^, is a special character in Regex, therefore it has to be escaped with a backslash i.e
replyText.replace(/\^/g, '')
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 3 years ago.
I have a string like
abcd/123/xyz/345
I want to replace every "/" with "-" using JavaScript.
The result string should be abcd-123-xyz-345
I have tried,
string.replace("/","-")
But it replaces the first "/" character only. The result is abcd-123/xyz/345
And
string.replace("///g","-");
is not working as well.
Is there any solution for this?
You can use Regex. You need to escape using backslash \ before the /.
A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally
var str = 'abcd/123/xyz/345';
let result = str.replace(/\//g,'-');
console.log(result);
Please try this,
var str='abcd/123/xyz/345'
var newstr=str.split('/').join('-');
console.log(newstr)
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create regex for checking if password got special characters.
https://www.owasp.org/index.php/Password_special_characters
It looks like this
new RegExp('[!##\$%\^\&*\)\(+=._\'",/<>?[\\\]`{|}~:;-\s]', 'g');
Unfortunately is also catching bare words: reg.test('word') it returns true.
Whats wrong with my regex?
You didn't escape properly. RegExp object could receive a regular expression as a string but escaping matters: it needs double backslashes.
For now [;-\s] is equal to [;-s] which includes 57 characters:
[_;?\[\]#\\`\^<->aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsS-Z]
However, it should be [;\-\\s].
You can use negative/inverse logic and test against any character that is not a number or a letter.
Using [^A-Za-z0-9] where caret (^) matches everything except A-Za-z0-9.
const regex = /([^A-Za-z0-9]|[.\p{L}])/gm;
console.log('word: ' + regex.test('word'));
console.log('word!: ' + regex.test('word!'));
console.log('!£2word!: ' + regex.test('!£2word!'));
console.log('!ą,ć,ó!"£wordąćó: ' + regex.test('!ą,ć,ó!"£wordąćó'));
This question already has answers here:
Regex to find not start and end with dot and allow some special character only not all
(3 answers)
Closed 2 years ago.
I am using following
ng-pattern="/^[a-zA-Z][a-zA-Z0-9._](.*[a-zA-Z0-9])?$/"
The matching String should
not start with a special character,
not end with special character, and
not include consecutive symbols except . (dot) and _ (underscore).
But it is not working.
Please, any suggestion.
Try using the word character class as a start ([\w] = [a-zA-Z0-9_]):
I'm not sure what you mean by consecutive symbols. But this might help:
/^[a-zA-Z]([\w.]*[a-zA-Z0-9])?$/
Maybe, have a look at the JavaScript RegExp Reference
This question already has an answer here:
Need to do a right trim on ajax query in javascript?
(1 answer)
Closed 8 years ago.
I wanted to check whether my string which ends with special character or not. If my string contains special character at the end, then need to trim at the right. if not, do nothing.
My piece of code:
var s = 'acbd#';
var x = 'abcd#e'
Expected Result:
acbd
abcd#e
any help on this?
You can use regular expression to replace the special characters with empty string, like this:
s.replace(/[#]+$/, "");
x.replace(/[#]+$/, "");
You can specify more special characters inside of square brackets.