js regex - get string between expressions - javascript

i want to match string in javascript, but take only part from matched string.
E.g i have string 'my test string' and use match function to get only 'test':
var text = 'my test string';
var matched = text.match(/(?=my).+(?=string)/g);
iam tried to get it in this way but it will return 'my test'.
How can i do this to get only 'test' with regex?

You can use a capture group:
var match = text.match(/my (.*) string/g);
# match[0] will be the whole string, match[1] the capture group
match[1];
this will still match the whole string, but you can get the contents with match[1].
Some other regex engines have a feature called "lookbehind", but this is unsupported in JavaScript, so I recommend the method with the capture group.

You need to change your regex to /my (.+) string/g and create RegExp object from it:
var regex = new RegExp(/my (.+) string/g);
Then use regex.exec(string) to get the capturing groups:
var matches = regex.exec(text);
matches will be an array with the value: ["my test string", "test"].
matches contains 2 groups: $0 and $1. $0 is the whole match, and $1 is the first capturing group. $1 it's what inside the parentheses: .+.
You need $1, so you can get it by writing matches[1]:
//This will return the string you want
var matched = matches[1];

Related

Remove part of word from a string with Javascript

I would like to find words in a sentence starting with a prefix and remove the rest of the characters.
Example:
this sentence Type_123 contains a Type_uiy
I would like to remove the characters that come after Type so I can have:
this sentence Type contains a Type
I know how I would go to remove the prefix with regex str.replace(/Type_/g,'') but how do I do the opposite action?
N.B. js prior ES6 if possible
Use the expression \b(Type)\w+ to capture the Type prefix.
Explanation:
\b | Match a word boundary (beginning of word)
(Type) | Capture the word "Type"
\w+ | Match one or more word characters, including an underscore
var str = 'this sentence Type_123 contains a Type_uiy';
var regex = /\b(Type)\w+/g;
console.log(str.replace(regex, '$1'));
The $1 in the replace() method is a reference to the captured characters. In this case, $1 stands for Type. So anywhere in the sentence, Type_xxx will be replaced with Type.
See MDN's documentation on the replace() method.
Install: https://github.com/icodeforlove/string-saw
let str = "Here's a sentence that contains Type_123 and Type_uiy";
let result = saw(str)
.remove(/(?<=Type_)\w+/g)
.toString();
The above would result in:
"Here's a sentence that contains Type_ and Type_"

How to capture group in Javascript Regex?

I am trying to capture customer.name from hello #customer.name from the end of the text string.
However, I can't seem to get the # character out of it. It gives #customer.name.
Right now my regex expression is:
#([0-9a-zA-Z.]+)$
Use the .match method of the string. The result will be null if there was no match with the given regex, otherwise it will be an array where:
The first element is the entire matched string, #customer.name in your case
The remaining elements are each capture group. You have one capture group so that would end up in the 1st index. In your case this will be customer.name, the string you want.
See this snippet. Your regex already looks correct, you just need to pull only the capture group from it instead of the entire matched string.
const str = "hello #customer.name"
const reg = /#([0-9a-zA-Z.]+)$/;
const match = str.match(reg)
console.log(match)
console.log("Matched string:", match[0]);
console.log("First capture group:", match[1]);
Your regex works fine, here's some code to use it to access your capture group using the regex .exec function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
let testString ='hello #customer.name',
pattern = /#([0-9a-zA-Z.]+)$/,
match = pattern.exec(testString);
console.log(match[1]); // abc

How to replace part of a string using regex

i need to replace a part of a string in Javascript
The following example should clarify what i mean
var str = "asd[595442/A][30327][0]";
var strToReplace = "30333";
var strDesiredResult = "asd[595442/A][30333][0]";
Basically it means the second area within the brackets should get replaced with another string
How to do that?
What i did so far is something like this :
var str = "asd[595442/A][30327][0]";
var regex = /asd\[(.*)\]\[(.*)\]\[(.*)\]/;
var arrMatches = regex.exec(str);
The string appears in arrMatches[2] correctly, and i could replace this. But what happens if in arrMatches[1] is the same string ?
Because it should only replace the value in the second bracket area.
You may use a regex that will match the first [....] followed with [ and capture that part into a group (that you will be able to refer to via a backreference), and then match 1+ chars other than ] to replace them with your replacement:
var str = "asd[595442/A][30327][0]";
var strToReplace = "30333";
console.log(str.replace(/(\[[^\]]*]\[)[^\]]*/, "$1" + strToReplace));
var strDesiredResult = "asd[595442/A][30333][0]";
console.log(strDesiredResult);
The /(\[[^\]]*]\[)[^\]]*/ has no gmodifier, it will be looking for one match only.
Since regex engine searches a string for a match from left to right, you will get the first match from the left.
The \[[^\]]*]\[ matches [, then any 0+ chars other than ] and then ][. The (...) forms a capturing group #1, it will remember the value that you will be able to get into the replacement with $1 backreference. [^\]]* matches 0+ chars other than ] and this will be replaced.
Details:
( - a capturing group start
\[ - a literal [ symbol (if unescaped, it starts a character class)
[^\]]* - a negated character class that matches zero or more (due to the * quantifier)
] - a literal ] (outside a character class, it does not have to be escaped)
\[ - a literal [
) - end of capturing group #1 (its value can be accessed with $1 backreference from the replacement pattern)
[^\]]* - 0+ (as the * quantifier matches zero or more occurrences, replace with + if you need to only match where there is 1 or more occurrences) chars other than ] (inside a character class in JS regex, ] must be escaped in any position).
Use this pattern:
'asd[595442/A][30327][0]'.replace(/^(asd\[[^\[\]]+\]\[)([^\[\]]+)(\]\[0\])$/, '$130333$3')
Test here
^ - match beginning of string
first group - match "asd[", any chars except [ and ], "]["
second group - match any chars except [ and ]
third group - match exactly: "][0]"
$ - match end of string
There are many ways to do this. One possible pattern is
str.replace(/^(.+)(\[.+\])(\[.+\])(\[.+\])$/, `$1$2[${strToReplace}]$4`)
You can see that $<number> is referred to captured string from regex (string groups in parentheses). We can refer to those and rearrange it however we want.
You can use Regular Expression like this /\[[0-9]+\]/ as below.
var str = "asd[595442/A][30327][0]";
var strToReplace = "30333";
var strDesiredResult = str.replace(/\[[0-9]+\]/, '[' + strToReplace + ']');
console.log(strDesiredResult); //"asd[595442/A][30333][0]";

js differentiate regexp capture groups

Is there a way in javascript/node.js to differentiate capture groups created by a regexp ? For example, this regexp: /([A-Z]+\[[^\]]+])|(\d+)/g on the text: 9 and 4687 matches but not NUMBER[9] or NUMBER[9568] However, [401] should match... will create two capture groups:
group 1: NUMBER[9] & NUMBER[9568]
group 2: 9, 4687 & 401
What I want is to add the tag NUMBER[] around numbers on my text. Is there a way to do something like text.replace(regexp, 'NUMBER[$&]', SECOND_GROUP) ?
EDIT: The output would be NUMBER[9] and NUMBER[4687] matches but not NUMBER[9] or NUMBER[9568] However, [NUMBER[401]] should match...
You may use a callback method as a replacement argument in String#replace() to check if a specific group matched, and perform appropriate replacement logic inside the anonymous method:
var regex = /([A-Z]+\[[^\]]+])|(\d+)/g;
var str = `9 and 4687 matches but not NUMBER[9] or NUMBER[9568] However, [401] should match...`;
var res = str.replace(regex, function($0,$1,$2) {
return $2 ? "NUMBER[" + $2 + "]" : $0; // If Group 2 matched, use special value, else paste back the match
});
console.log(res);
The $0 stands for the whole match, $1 represents Group 1 contents, and $2 refer to the Group 2 contents.
You may add more logic to also account for specific Group 1 match treatment.

Javascript Regex match any word that starts with '#' in a string

I'm very new at regex. I'm trying to match any word that starts with '#' in a string that contains no newlines (content was already split at newlines).
Example (not working):
var string = "#iPhone should be able to compl#te and #delete items"
var matches = string.match(/(?=[\s*#])\w+/g)
// Want matches to contain [ 'iPhone', 'delete' ]
I am trying to match any instance of '#', and grab the thing right after it, so long as there is at least one letter, number, or symbol following it. A space or a newline should end the match. The '#' should either start the string or be preceded by spaces.
This PHP solution seems good, but it uses a look backwards type of functionality that I don't know if JS regex has:
regexp keep/match any word that starts with a certain character
var re = /(?:^|\W)#(\w+)(?!\w)/g, match, matches = [];
while (match = re.exec(s)) {
matches.push(match[1]);
}
Check this demo.
let s = "#hallo, this is a test #john #doe",
re = /(?:^|\W)#(\w+)(?!\w)/g,
match, matches = [];
while (match = re.exec(s)) {
matches.push(match[1]);
}
console.log(matches);
Try this:
var matches = string.match(/#\w+/g);
let string = "#iPhone should be able to compl#te and #delete items",
matches = string.match(/#\w+/g);
console.log(matches);
You actually need to match the hash too. Right now you're looking for word characters that follow a position that is immediately followed by one of several characters that aren't word characters. This fails, for obvious reasons. Try this instead:
string.match(/(?=[\s*#])[\s*#]\w+/g)
Of course, the lookahead is redundant now, so you might as well remove it:
string.match(/(^|\s)#(\w+)/g).map(function(v){return v.trim().substring(1);})
This returns the desired: [ 'iPhone', 'delete' ]
Here is a demonstration: http://jsfiddle.net/w3cCU/1/

Categories

Resources