I am trying to extract the substring between 3rd occurance of '|' character and ';GTSet' string within string
For Example, if my string is "AP0|#c7477474-376c-abab-2990-918aac222213;L0|#0a4a23b12-125a-2ac2-3939-333aav111111|ABC xxx;pATeND|#222222ANCJ-VCVC-2262-737373-3838383";
I would like to extract "ABC xxx" from above string using javascript.
I have tried following options
var str = "AP0|#c7477474-376c-abab-2990-918aac222213;L0|#0a4a23b12-125a-2ac2-3939-333aav111111|ABC xxx;pATeND|#222222ANCJ-VCVC-2262-737373-3838383";
alert(str.match(/^|\;pATeND(.*)$/gm));
//var n = str.search(";pATeND");
//to get the 3rd occurance of | character
//var m = str.search("s/\(.\{-}\z|\)\{3}");
This lookahead regex should work:
/[^|;]+(?=;pATeND)/
RegEx Demo
Or if paTeND text is know known then grab the value after 3rd |:
^(?:[^|]*\|){3}([^|;]+)
and use captured group #1.
Demo 2
Related
I have a string in the following format and I'm trying to return all the data after the 3rd occurrence of the ':' character as in the example below.
user_name_1, 10:46:36 activity_name_1 : the text to be returned
So far I have the regex \:.* that returns everything after the first occurrence, eg. :46:36 activity_name_1 : the text to be returned
If I modify it to \:{3}.* eg. to look for the 3rd instance, the regex will not return any matches. It looks like it should be a very simple query but nothing I've tried seems to work.
I've already found the following question find value after nth occurence of - using RegEx however in this case they're returning only the next 3 digits after the nth character and not the entire remaining string.
You can use
^(?:[^:]*:){3}\s*(\S.*)
See the regex demo. Details:
^ - start of string
(?:[^:]*:){3} - three occurrences of any zero or more chars other than a : and then a : char
\s* - zero or more whitespaces
(\S.*) - Group 1: a non-whitespace char and then the rest of the line.
See the JavaScript demo:
const text = "user_name_1, 10:46:36 activity_name_1 : the text to be returned";
const match = text.match(/^(?:[^:]*:){3}\s*(\S.*)/)
if (match) {
console.log(match[1])
}
I'd suggest not using regex for this. split() the string by the : character and remove the first two elements of the resulting array.
You can turn the result back in to a string if necessary by using join():
let input = 'user_name_1, 10:46:36 activity_name_1 : the text to be returned : foo : bar';
let arr = input.split(':');
arr.splice(0, 3);
console.log(arr);
let output = arr.join(':').trim();
console.log(output);
i was experimenting on regex in javascript. Then i came across an issue such that let consider string str = "+d+a+", I was trying to output those characters in the string which are surrounded by +, I used str.match(/\+[a-z]\+/ig), so here what I'm expecting is ["+d+","+a+"], but what i got is just ["+d+"], "+a+" is not showing in the output. Why?
.match(/.../g) returns all non-overlapping matches. Your regex requires a + sign on each side. Given your target string:
+d+a+
^^^
^^^
Your matches would have to overlap in the middle in order to return "+a+".
You can use look-ahead and a manual loop to find overlapping matches:
var str = "+d+a+";
var re = /(?=(\+[a-z]\+))/g;
var matches = [], m;
while (m = re.exec(str)) {
matches.push(m[1]);
re.lastIndex++;
}
console.log(matches);
With regex, when a character gets consumed with a match, then it won't count for the next match.
For example, a regex like /aba/g wouldn't find 2 aba's in a string like "ababa".
Because the second "a" was already consumed.
However, that can be overcome by using a positive lookahead (?=...).
Because lookaheads just check what's behind without actually consuming it.
So a regex like /(ab)(?=(a))/g would return 2 capture groups with 'ab' and 'a' for each 'aba'.
But in this case it just needs to be followed by 1 fixed character '+'.
So it can be simplified, because you don't really need capture groups for this one.
Example snippet:
var str = "+a+b+c+";
var matches = str.match(/\+[a-z]+(?=\+)/g).map(function(m){return m + '+'});
console.log(matches);
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
<li class="item">hello?james</a></li>
<li class="item">goodbye?michael</a></li>
I want to extract the text that is after the sign "?" -> james, michael
I tried using a substring method but it only works if I specify the starting and the ending like substr(5,10) or substr(5) etc.
I'm using this when I extract from another file in a foreach php method, so I need everything that is after "?".
Is there any method in which I can substring starting with a character (eg. "?") or a specific string ?
Many thanks!
Use a regex with capture groups
Regular expressions will make it easy to match your HTML and using capture groups (parenthesis) you can extract the names from the string:
var myString = YOUR_HTML_HERE;
var myRegexp = /<li .*\?([a-zA-Z0-1]*)</g;
var match = myRegexp.exec(myString);
alert(match[1]); // james.
Execute the regex multiple times to get all matches
You can run the regex again in the original string to get the next match. To get all of them, do this:
while (match != null) {
match = myRegexp.exec(myString);
alert(match[1]); // michael
}
i want to replace last input character from keyboard to ''
My String Input are
sample string
"<p><strong>abscd sample text</strong></p>"
"<p>abscd sample text!</p>"
My last character is dynamic that can be any thing between
a to z, A to Z, 0 to 9, any special characters([~ / < > & ( . ] ).
So i need to replace just that character
for example in Sample 1 i need to replace "t" and in sample 2 in need to replace "!"
I tried below code. but it id not worked for me
var replace = '/'+somechar+'$/';
Any way to do it?
Step one
to replace the a character in a string, use replace() function of javaScript. Here is the MDN specification:
Returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.
Step two
you need to location the character to be replaced through regular expression. You want to replace the last character of a string and this could be expressed as /(.+)(.)$/. . stands for any character, + means more than one character. Here (.+) matches all the character before the last one. (.) matches the last character.
What you want to replace is the one inside the second brackets. Thus you use the same string matched in the first bracket with $1 and replace whatever after it.
Here is the code to realize your intention:
text = 'abscd sample text';
text.replace(/(.+)(.)$/, '$1!');
Do you really need to use regular expressions? How about str = str.slice(0, -1); ? This will remove the last character.
If you need to replace a specific character, do it like this:
var replace = new RegExp(somechar + '$');
str = str.replace(replace, '');
You cannot use slashes in a string to construct a RegEx. This is different from PHP, for example.
I dont really understand which character you want to replace to what, but i think, you should use replace() function in JS: http://w3schools.com/jsref/jsref_replace.asp
string.replace(regexp/substr,newstring)
This means all keyboard character:
[\t\n ./<>?;:"'`!##$%^&*()[]{}_+=-|\\]
And this way you can replace all keyboard character before < mark to ""
string.replace("[a-zA-Z0-9\t\n ./<>?;:"'`!##$%^&*()[]{}_+=-|\\]<","<")