Regex: strip out if not "OBX" - javascript

I have data like this:
MSH|1|data1|data2|data3
PID|1|data5|data6|data7
PVI|1|data2|data2|data2
OBX|1|data0|data4|data9
OBX|2|data8|data8|data9
OBX|3|data1|data1|data1
I am trying regex to strip out any lines that don't start with OBX. Here's what I have so far:
message = message.replace(/^(?!OBX).+/g, '');
Even though I have /g it only triggered on the first. Is there something else I'm missing?

Since you are using anchor ^ in your regex, you will need to use m flag (MULTILINE):
message = message.replace(/^(?!OBX).+/gm, '');
Without m modifier your regex will match ^ only at the start of very first line instead of matching before every line.
RegEx Demo

Related

How to capture a subdomain in regex?

I'm trying to extract the username from a tumblr. That is, the regex should match asdf in the following test string:
https://asdf.tumblr.com/
http://asdf.tumblr.com/faq
www.asdf.tumblr.com/
asdf.tumblr.com
Basically, I think I need to do something like, match from either a dot or a slash until the next dot, but I'm having trouble making it work in every case. Currently I have this:
.*[\/|\.](.*)\.tumblr\.com.*
However, this fails to capture the last group (asdf.tumblr.com). I tried modifying it to no avail. Can this be done?
You may use this regex in Javascript:
/[^.\/]+(?=\.tumblr\.com)/i
RegEx Demo
RegEx Details:
[^.\/]+: Match 1 or more of any character that is not . and /
(?=\.tumblr\.com): Positive lookahead to ensure we have .tumblr.com at next position
Code:
let x = /([^.\/]+)(?=\.tumblr\.com)/;
let y = "https://asdf.tumblr.com";
console.log( y.match(x)[1] );

RegExp Match only with paths contains filename

I am trying to create a Regexp in JS that only look into string having files and ignore the paths which don't have a filename.
input
tanxe\src\lib\hello
tanxe\lib\hello\world\verboseBackup.js
tanxe\src\hello\verboseBackup.js
tanxe\lib\verboseBackup.js
Trying
input.match(/^tanxe.*?lib.*?\\..*/i)
Expected Output:
tanxe\lib\hello\world\verboseBackup.js
tanxe\lib\verboseBackup.js
You can try this mate
^.*\.\w+$
Explanation
^ - Anchor to start of string.
.* - Matches anything one or more times except newline character.
\. - Matches ..
\w+ - Matches word character one or more time.
$ - End of string.
Demo
Update:- In case you strictly want to match tanxe\lib and followed things only
You can try this mate
^tanxe\\lib\\.+\.\w+$
Demo
You might try this: tanxe\\lib.*?\.\w+
It matches paths starting with tanxe\lib and ending with a file extension.
input.match(/^tanxe\\lib\\(\w+\\)*\w+\.\w+/gi);
See the regExr fiddle I created.
Your regex is work, I think you need is additional flags: g global, m multiline
var input = `tanxe\\src\\lib\\hello
tanxe\\lib\\hello\\world\\verboseBackup.js
tanxe\\src\\hello\\verboseBackup.js
tanxe\\lib\\verboseBackup.js
D:\\Program Files\\atom\\.atom\\packages\\utilities-tanxe\\lib\\abc\\verboseBackup.js`
input.match(/^.*tanxe.*?lib.*?\..*/gmi).forEach(r => console.log(r))
// start with "tanxe"
//input.match(/^tanxe.*?lib.*?\..*/gmi).forEach(r => console.log(r))
Try this one too.
tanxe\\[a-zA-Z\\]+[.]{1}[a-zA-z]{2,3}

How to replace last part of URL using Regex and jQuery?

I'm not using REGEX very often so I don't know it well.
Want to match last digits before / end of string.
so my regex will be\d+/$
Now I want to replace matched part of href inside the link.
First thing
SyntaxError: illegal character
var regex = \d+/$
so I escaped it (I think) var regex = /\d+//$
I thought it will be simple from now:
$('a').attr('href').replace(regex,'00/')
But it seems no use.
I'm using firebug console for testing
Solution
url = "www.example.com/event/detail/46/"
var value = url.substring(url.lastIndexOf('/') + 1);
url = url.replace(value, '00')
What you seem to want is this :
$('a').attr('href', function(_,h){ return h.replace(/\d+\/$/,'00/') });
A slash is escaped as \/ in a regex literal, not as //.
$(selector).attr(name, fun) will apply the function to each element.
In escaping use \ not /.
So this will be
var regex = /\d+\$/

RegEx - Get All Characters After Last Slash in URL

I'm working with a Google API that returns IDs in the below format, which I've saved as a string. How can I write a Regular Expression in javascript to trim the string to only the characters after the last slash in the URL.
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9'
Don't write a regex! This is trivial to do with string functions instead:
var final = id.substr(id.lastIndexOf('/') + 1);
It's even easier if you know that the final part will always be 16 characters:
var final = id.substr(-16);
A slightly different regex approach:
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
Breaking down this regex:
\/ match a slash
( start of a captured group within the match
[^\/] match a non-slash character
+ match one of more of the non-slash characters
) end of the captured group
\/? allow one optional / at the end of the string
$ match to the end of the string
The [1] then retrieves the first captured group within the match
Working snippet:
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9';
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
// display result
document.write(afterSlashChars);
Just in case someone else comes across this thread and is looking for a simple JS solution:
id.split('/').pop(-1)
this is easy to understand (?!.*/).+
let me explain:
first, lets match everything that has a slash at the end, ok?
that's the part we don't want
.*/ matches everything until the last slash
then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"
(?!.*) this is "Negative lookahead"
Now we can happily take whatever is next to what we don't want with this
.+
YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:
(?!.*\/).+
this regexp: [^\/]+$ - works like a champ:
var id = ".../base/nabb80191e23b7d9"
result = id.match(/[^\/]+$/)[0];
// results -> "nabb80191e23b7d9"
This should work:
last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9
Don't know JS, using others examples (and a guess) -
id = id.match(/[^\/]*$/); // [0] optional ?
Why not use replace?
"http://google.com/aaa".replace(/(.*\/)*/,"")
yields "aaa"

Delete line starting with a word in Javascript using regex

I have few lines on text.
Random 14637547548546546546sadas3463427
Random 1463754754854654654sadsa63463427
Macroflex 1463754754854654sada65463463427
Random 146375475485465465sdas463463427
Random 1463754754854654fdasf65463463427
I would like to find a line what starts with Macroflex (in this case) and replace/delete it. This is what I have so far... I have tried over and over with regex, but it makes my head hurt. Can anyone give me an advice?
var myRegex = data.replace('Macroflex', '')
You have to replace to the end of the line:
var myRegex = data.replace(/^Macroflex.*$/gm, '');
Note that you have to specify the m flag to let ^ and $ work with newlines.
If you want to remove the newline after the line, you can match it:
var myRegex = data.replace(/^Macroflex.*\n?/gm, '');
This works since . does not match newlines.
The /g flag enables removing multiple occurrences of the line.

Categories