regex - Pattern match in javascript -
I can not find the right result in the code given below How can I match pattern in javascript?
function getPathValue (url, input) {console.log ("This is the path:" + input); Url = url.replace (/% 7C / g, '|'); Var input = input. Split ("|"); If (InputRLTT1) input = '\\ b' + Inputer [0] + '\ n |' + Input [1] + '\\ b'; And input = '\\ b' + input + '\' b '; Var field = url.search (input); Var slash 1 = url.indexOf ("/", field); Var slash2 = url.indexOf ("/", slash 1 + 1); If (slash2 == -1) slash2 = url.exex ("?"); If (slash 2 == -1) slash2 = url Laugh; Console.log ("This path is the absolute value:" + url.substring (slash1 + 1, slash2)); Return url.substring (slash 1 + 1, slash 2); } GetPathValue ("http: // localhost / responsepath / mountainwithpassid | accesscode / 100 / mountainwithpassid | passid / 1", "mountainwithpassid | passid")
I am getting the output below < Br />
If I go to the mountain range As an access code, input I is getting output as 100. Similarly if I pass
key: mountainwithpassid | Passid
Value: 100 / / expected output 1
If you intend only To achieve the value in that path, the input follows the input (contained in '/'), then you can get it with a simple regular expression before you will need a method to avoid your input string, because in it A pipe character '|'
RegExp.escape = function (s) {return s.replace (/ [- \ / \\ ^ $ * +. (.) | [\] {}] / G, \\ $ & amp;;); };
Then your getPathValue
function might look like this:
function getPathValue (url, input) {var pathValue = Null; Var escapedInput = RegExp.escape (input); / / Removes the value following the RegExp input below and // is inside '/' characters (last '/' is optional) var pathValueRegExp = New RegExp (". *" * + EscapedInput + "(/ [^ / ] +) /?.* ", 'g'); If (pathValueRegExp.test (url)) {pathValue = url.replace (pathValueRegExp, '$ 1'); } Return path value; }
You will also need to think about how to handle errors - in case if no mail is found then a zero value will be returned.
Comments
Post a Comment