Javascript regex to remove number, then a space? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How would I go about removing numbers and a space from the start of a string?
For example, from '13 Adam Court, Cannock' remove '13 '.

Search for
/^[\s\d]+/
Replace with the empty string. Eg:
str = str.replace(/^[\s\d]+/, '');
This will remove digits and spaces in any order from the beginning of the string. For something that removes only a number followed by spaces, see BoltClock's answer.

str.replace(/^\d+\s+/, '');

var text = '13 Adam Court, Cannock';
var match = /\d+\s/.exec(text)[0];
text.replace(match,"");

The above solution doesn't work for me. Instead I used this regex below.
var sRegExResult = "Regex Sample99";
sRegExResult.replace(/\s|[0-9]/g, '');

Related

Can I use regular expression to match two a characters only one time? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can I split string by regex to match two parameters (: , ) only one time?
let str='part1:20/02/2002 part3 part4 part5';
let splittedArr=str.split(something..);
I would like the splittedArr to look like:
[part1 , 20/02/2002 , part3 part4 part5]
in js you can split on a regex
and the regex is fairly simple its ":" or " "
the or in regex is a |
let str='part1:20/02/2002 part3 part4 part5';
let splittedArr=str.split(/:| /);
console.log(splittedArr);
EDIT
I've misinterpreted the question, if I understand your question correctly;
You want to split on the first colon (:) and on the first space ( ), and capture those 3 parts.
You can achieve this with simple group matches, and ungreedy wildcards.
let str='part1:20/02/2002 part3 part4 part5';
let parts=str.match('^(.*?):(.*?) (.*)$');
console.log(parts);

Javascript - Regex Does not contain some character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I try to regex for not contain some character.
I need to show /%7(.*?);/g which dose not contain "=".
I try to input
?!xx=1
and change to
?!( (.?)=(.?) )
But it dose not work.
Please help. Thanks.
//Here is my simple regex
reg = /%7((?!xx=1).*?);/g ;
//Here is my string
str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
//I need
%7aa; and %7yy;
Instead of using a negative lookahead, try using a ^ block:
const reg = /%7([^=;]+);/g;
The ([^=;]+) bit matches any non-=, the condition you're looking for, and non-;, the character at the end of your regex.
I left the capture group in since your question's regex also contains it.
const reg = /%7([^=;]+);/g;
const str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
const matches = str.match(reg);
console.log(matches);

Replace comparation with capital letters on Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I don't know how to replace with capital letters in this input text, for example I want to put on bold the letters which they are de same on the input, but if is capital letters doesn't put on bold
this is my line of code $texto_option = $texto_option.replace($(input).val(),'<b>'+$(input).val()+'</b>');
Generate a regular expression that ignores the letters' case:
var search = $(input).val();
var re = new RegExp(search, "i");
$texto_option = $texto_option.replace(re, "<b>$&</b>");
This is an easy answer. But keep in mind that your input has to be sanitized, as some characters are control characters for regular expressions too:
search = search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

Get regex rules for a capture group [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a method to get the regex rules, for a capture group.
So if I had: /(\w) (\d)/ I would want $1 = /\w/ and $2 = /\d/.
Is there a method for this?
Provided that the regex is valid and no nested groups are used, then you can try this out.
var arr = (regex + "").match(/\(.*?\)/g).map(function(rule){
return rule.replace(/[()]/g, "");
});
Now, arr[0] will have rule of group1, arr[1] will be group2 and so on.
Although I don't know of any direct way to do this on a regex rule given by /(rule1)(rule2)/ you could build the regex rule using strings and specify your grouping in different strings.
var group1 = '([A-Za-z]+)',
group2 = '(\\d+)',
r = new RegExp(group1+group2);
r.test('hello1234');
To get the groups from a regex string you could run a regex on that regex string to extract the groups. If your regex string is "(\w+)(\d+)" then you'd have a regex to extract the groups as /(\([^\)\))+/

Capturing everything in a string with a regular expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have a string that will have basically any kind of text in it and I want to capture all the text with regular expression and replace it.
var img_url = "http://grfx.domain.com/photos/dir/school/dir/m-dir/auto_original/123456.jpeg?123456";
img_url.replace(/regexp/,newvalue);
Just trying to capture everything in the img_url var and replace it.
Thanks!
Well, if you want to replace everything, you don't need regex. Simply write:
img_url = newvalue;
If you are really determined to do this with a regular expression, this could help:
img_url = img_url.replace(/.*/, newvalue);
If you're reassigning, just use =
If you want to replace all matches in one go, use the g flag, in which case this question is a duplicate of this post
If that expression needs to be dynamic:
var expr = new RegExp(replaceVar,'gi');//globally, and case-insensitively
var newUrl = oldUrl.replace(expr,replacement);
Note that, here, all back-slashes like you'd use for \b have to be escaped, too: \\b and \\/ for forward slashes. and \\\\ for an escaped backslash

Categories

Resources