This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Hi I'm trying to remove the letters and special characters from javascript.
Example:
var id = "Parameter[0].Category"
I only need the "0" from this. Thank you
Try this
var numbers = id.match(/\d+/)[0];
console.log(numbers);
In general this is called filtering using regular expressions.
If you want to filter out letters and special characters you can use regular expression in JS, like this:
id = id.replace(/\D/g, "")
You replace every (g option) character in your string that is not a digit \D with blank ""
Related
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Trying to replace a string that contains special characters. The purpose of this is to convert the query string into an understandable format for end users.
full string is:
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
Specifically the portion after ^NQ, in this example: opened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday(). I have split the original string with indexOf(^NQ) and passing the resulting sub-strings to a function. I'm then trying a .replace() as below:
var today = replacementString.replace(/(ONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday())/g, ' is today ');
replacementString = today;
I have tried with various combinations of the above line, but have not returned what I am hoping for.
I've had no issues replacing special characters, or strings without special characters, but the combination of the 2 is confusing/frustrating me.
Any suggestions or guidance would be appreciated
You should escape the () to \(\) to match it literally or else it would mean a capturing group. For the match you could also omit the outer parenthesis and you have to escape the dot \. to match it literally.
ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
var today = str.replace(/ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)/g, ' is today ');
replacementString = today;
console.log(today);
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I've got a bunch of strings to browse and find there all words which contains "(at)" characters and then gather them in the array.
Sometimes is a replacement of "#" sign. So let's say my goal would be to find something like this: "account(at)example.com".
I tried this code:
let gathering = myString.match(/(^|\.\s+)((at)[^.]*\.)/g;);
but id does not work. How can I do it?
I found a regex for finding email addresses in text:
/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)
I think about something similar but unfortunately I can't just replace # with (at) here.
var longString = "abc(at).com xyzat.com";
var regex = RegExp("[(]at[)]");
var wordList = longString.split(" ").filter((elem, index)=>{
return regex.test(elem);
})
This way you will get all the word in an array that contain "at" in the provided string.
You could use \S+ to match not a whitespace character one or more times and escape the \( and \):
\S+\(at\)\S+\.\w{2,}
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How can I create a regex for that returns true if it has only numbers and '+' basically 0-9 & +. Using javascript or jQuery.
Regex for plus anywhere: /^[0-9+]+$/
Regex for plus only infront: /^\+?[0-9]+$/
What it does:
^ Matches the beginning of the string
[0-9+] Matches 0123456789+
+ Matches one or more
$ Matches the end of the string
Other version:
\+? Matches zero or one plus signs in the front
Maybe try regexr for future regex development.
How to test in code:
function isOnlyNumber(str) {
return /^[0-9+]+$/.test(str);
}
This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 6 years ago.
I'm trying to write a regex expression to match multiple characters such as , OR . OR : OR ( OR )
I have this
removePunctuation = /\.$|\,$|\:|\(|\)/;
word = rawList[j].replace(removePunctuation,"")
I just want to remove periods & commas at the end of the sentence but all instances of ( ) :
What am I doing wrong?
You need to add the "g" qualifier if you want to remove all the punctuation.
removePunctuation = /\.$|\,$|\:|\(|\)/g;
I'd go with something like this:
/[.,]+$|[:()]+/g
It'll match periods and commas at the end of a sentence, and brackets and colons everywhere.
This question already has answers here:
Regular expression, How to allow combination of dot (period) and letters?
(7 answers)
Closed 3 years ago.
from regexp=^(?:[a-zA-Z]+(?:[.'\-,])?\s?)+$, how am I suppose to do it allowing only alphanumeric and dots? Thanks!
Try this:
regexp = ^[a-zA-Z0-9\.]+$
This will get alphnum and dots..
/[a-zA-Z0-9.]/
In javascript the regular expressions shouldn't be in quotes, but in slashes. For exapmple:
var myregexp = /[a-z0-9\.]+/i;
var myvar = "I am a regular expression.";
var result = myregexp.test(myvar);
returns true, because in myvar there are only digits, letters and dots.
Note: the dots are special symbols and must be preceded by \