I'm trying to extract a number and text from strings like those: 171Toberin, [171]Toberin or [171] Toberin.
I have this RegExp /(?<code>\d+)(?<name>\w+)/u, and this RegExp only works with 171Toberin.
You can use the below regex to remove all non alphanumeric characters.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.replace(/[^a-zA-Z0-9]/g, ''));
Or use the below to extract the alphanumeric characters from string.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.match(/[a-zA-Z0-9]+/g));
Or if you want to extract numbers and strings in separate array then use the below one.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.match(/[0-9]+/g));
console.log(string.match(/[a-zA-Z]+/g));
Please try with this: (?<code>\d+)[^\d\w]*(?<name>\w+)/ug
It works with the entire sentence: 171Toberin, [171]Toberin or [171] Toberin.
Returning 3 matches. You can try it at https://regex101.com/
With [^\d\w]* you omit possible numbers and words in between. With g flag you return all matches.
Related
How do I write a regex that accepts only words or letters and split them by ,?
I have tried array = input.replace(/ /g, '').split(','), but then h-e,a<y will become ['h-e','a<y'] I want to accept only variables, so I guess h-e,a<y should become ['he','ay'].
Would it be something like
array = input.replace(/[\s|^\w]/g, '').split(',')
You could use this regex to find all the characters that you want to remove from your string:
array = input.replace(/[-><?.:;]*/ig, '').split(',')
You will replace all the characters that are inside the [ ].
Split first, then remove characters not allowed:
input.split(,).map(fix)
where
function fix(s) {
return s.replace(/[^\w$]/g, '');
}
Another approach is to grab the characters you want, instead of throwing away the ones you don't:
function fix(s) {
return s.match(/[\w$]/g).join('');
}
Actually, this is not exactly right, because JavaScript variable names can also contain Unicode characters such as Σ. Also, this would not fix up leading numeric characters, which JavaScript variable names cannot start with.
I've got this css string
var cssString = 'scale(0.95,0.95) rotate(45deg) translate(10px,11px)';
I want to use regex to return an array looking like this:
var array = ['scale(','0.95',',','0.95',') rotate(','45','deg) translate(','10','px,','11','px)'];
my last attempt:
var array = cssString.match(/([ a-zA-Z])*(\d*[.]?\d*)*/g);
I'm struggeling to have the first group match the parantheses aswell. How do I make this happen?
This regex will separately select both text (with special characters ()) and numbers (with dots .) and the commas:
([A-Za-z()])+|(,)+|([.\d])+
I have this string: 2015-07-023. I want to get 07 from this string.
I used RegExp like this
var regExp = /\(([^)]+-)\)/;
var matches = regExp.exec(id);
console.log(matches);
But I get null as output.
Any idea is appreciated on how to properly configure the RegExp.
The best way to do it is to not use RegEx at all, you can use regular JavaScript string methods:
var id_parts = id.split('-');
alert(id_parts[1]);
JavaScript string methods is often better than RegEx because it is faster, and it is more straight-forward and readable. Any programmer can read this code and quickly know that is is splitting the string into parts from id, and then getting the item at index 1
If you want regex, you can use following regex. Otherwise, it's better to go with string methods as in the answer by #vihan1086.
var str = '2015-07-023';
var matches = str.match(/-(\d+)-/)[1];
document.write(matches);
Regex Explanation
-: matches - literal
(): Capturing group
\d+: Matches one or more digits
Regex Visualization
EDIT
You can also use substr as follow, if the length of the required substring is fixed.
var str = '2015-07-023';
var newStr = str.substr(str.indexOf('-') + 1, 2);
document.write(newStr);
You may try the below positive lookahead based regex.
var string = "2015-07-02";
alert(string.match(/[^-]+(?=-[^-]*$)/))
I have the below String value to be displayed in text area and i want to remove the first characters ##*n|n from the string .
The string is as follows :
Symbol-001
##*n|nClaimant Name
##*n|nTransaction
I have used the below code to deal with removing the special characters
var paramVal1 = parent.noteText; //paramVal1 will have the string now
var pattern = /[##*n|n]/g;
var paramVal1 = paramVal1.replace(pattern,'');
document.getElementById("txtNoteArea").value = paramval1;//appending the refined string to text area
For the above used code am getting the out put string as below
Symbol-001
|Claimat Name //here 'n' is missing and i have an extra '|' character
|Transactio //'n' is missing here too and an extra '|' character
Kindly help to remove the characters ##*n|n without affecting the other values
What your regex is saying is "remove any of the following characters: #|*n". Clearly this isn't what you want!
Try this instead: /##\*n\|n/g
This says "remove the literal string ##*n|n". The backslashes remove the special meaning from * and |.
You are using regular expression reserved chars in your pattern, you need to escape them
You can use this expression:
var pattern = /[\#\#\*n\|n]/g;
i think use this /[##*n\|n]/g regEx
If you want to replace the first occurrence as you say on your question, you don't need to use regex. A simple string will do, as long as you escape the asterisk:
var str = "Symbol-001 ##*n|nClaimant Name ##*n|nTransaction";
var str2 = str.replace("##\*n|n", ""); //output: "Symbol-001 Claimant Name ##*n|nTransaction"
If you want to replace all the occurrences, you can use regex, escaping all the characters that have a special meaning:
var str3 = str.replace(/\#\#\*n\|n/g, ""); //output: "Symbol-001 Claimant Name Transaction"
Have a look at this regex builder, might come in handy - http://gskinner.com/RegExr/
string str contains somewhere within it http://www.example.com/ followed by 2 digits and 7 random characters (upper or lower case). One possibility is http://www.example.com/45kaFkeLd or http://www.example.com/64kAleoFr. So the only certain aspect is that it always starts with 2 digits.
I want to retrieve "64kAleoFr".
var url = str.match([regex here]);
The regex you’re looking for is /[0-9]{2}[a-zA-Z]{7}/.
var string = 'http://www.example.com/64kAleoFr',
match = (string.match(/[0-9]{2}[a-zA-Z]{7}/) || [''])[0];
console.log(match); // '64kAleoFr'
Note that on the second line, I use the good old .match() trick to make sure no TypeError is thrown when no match is found. Once this snippet has executed, match will either be the empty string ('') or the value you were after.
you could use
var url = str.match(/\d{2}.{7}$/)[0];
where:
\d{2} //two digits
.{7} //seven characters
$ //end of the string
if you don't know if it will be at the end you could use
var url = str.match(/\/\d{2}.{7}$/)[0].slice(1); //grab the "/" at the begining and slice it out
what about using split ?
alert("http://www.example.com/64kAleoFr".split("/")[3]);
var url = "http://www.example.com/",
re = new RegExp(url.replace(/\./g,"\\.") + "(\\d{2}[A-Za-z]{7})");
str = "This is a string with a url: http://www.example.com/45kaFkeLd in the middle.";
var code = str.match(re);
if (code != null) {
// we have a match
alert(code[1]); // "45kaFkeLd"
}
The url needs to be part of the regex if you want to avoid matching other strings of characters elsewhere in the input. The above assumes that the url should be configurable, so it constructs a regex from the url variable (noting that "." has special meaning in a regex so it needs to be escaped). The bit with the two numbers and seven letter is then in parentheses so it can be captured.
Demo: http://jsfiddle.net/nnnnnn/NzELc/
http://www\\.example\\.com/([0-9]{2}\\w{7}) this is your pattern. You'll get your 2 digits and 7 random characters in group 1.
If you notice your example strings, both strings have few digits and a random string after a slash (/) and if the pattern is fixed then i would rather suggest you to split your string with slash and get the last element of the array which was the result of the split function.
Here is how:
var string = "http://www.example.com/64kAleoFr"
ar = string.split("/");
ar[ar.length - 1];
Hope it helps