codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

javascript regex .match stop at a certain string


By : , Category : javascript

don't know if this is the best way, but found something called a Positive Lookahead:

var string = "http://website.com/#!/goodText=hello/hello//stopHere=badtext";    
var reg = string.match(/goodText=.*(?=/stopHere)/);
// returns: goodText=hello/hello/

basically if I put (?=regex) at the end, it stops when it finds that!

ReLated :

[^stop] doesn't means any text that is NOT stop. It just means any character that is not one of the 4 characters inside [...] which is in this case s,t,o,p.

Better to split on the text you don't want to match:

$s = 'fdjkaljfdlstopfjdslafdjstopfoobar';

php> $arr = preg_split('/stop/', $s);

php> print_r($arr);
Array
(
    [0] => fdjkaljfdl
    [1] => fjdslafdj
    [2] => foobar
)

You mean like this?

var test = "This is a string t:1h,2h,3h"
var matches = test.match(/t:.*/)
console.debug(matches[0])

Gives

t:1h,2h,3h
var resultRE = /((d+)(?:, (d+))?(?:, (d+))?)Rerun/;
if (resultRE.test(resultLine)) {
  var num1 = RegExp.$1,
  num2 = RegExp.$2,
  num3 = RegExp.$3;
}

You can use array#filter:

var matches = text.match(/.*
?/g).filter(Boolean);
//=> [ 'abcd
', 'efg
', '
', 'hijk' ]

Or using a slightly different regex with non-optional (but it assumes new line is always there after last line):

var matches = text.match(/.*
/g);
//=> [ 'abcd
', 'efg
', '
', 'hijk' ]
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .