DWQA QuestionsCategory: ProgramJavaScript regular global match extraction problem / selectnull \ (\ '([\ W -] +) \' \) / Ig
util asked 1 month ago

demand
from

selectNull('AgentIdI333');selectNull('GameId333');
if(this.value) 
selectChange(jsonUrl.myAgent+this.value,'AgentIdI333','','AGENTID','AGENTNAME')

Extract from

["AgentIdI333","GameId333"]

regular

/selectNull\(\'([\w-]+)\'\)/ig 

test

<select name="AgentIdP" id="AgentIdP333" class="customTAB"
onchange="selectNull('AgentIdI333');selectNull('GameId333');if(this.value) selectChange(jsonUrl.myAgent+this.value,'AgentIdI333','','AGENTID','AGENTNAME')">
</select>


<script type="text/javascript">
var html = document.getElementById('AgentIdP333').getAttribute('onchange');
// var m = html.match(/selectNull\(["\']{1}([\w]+)[\'"]{1}\)/ig);
var m = html.match(/selectNull\(\'([\w-]+)\'\)/ig);
//["selectNull('AgentIdI333')", "selectNull('GameId333')"]

console.log(m);
</script>


supplement
Now, you can get all of them by using while loop in JavaScript.

2 Answers
Childe answered 1 month ago
util replied 1 month ago

I used a can implement while ((VaR result = pattern. Match (HTML))! = null)

JerryZou answered 1 month ago

How to use regular extraction, @ childe has given a good answer, but I have some different opinions on the landlord’s doing so.
Look at the owner’s codeAgentIdP333andGameId333Are two more important attributes, or333Is an important value.
On the one hand, the value of this attribute is written in the code for the time being, and theonchangeIt’s too much to extract from a function.
Instead, use the data attribute:

<select name="AgentIdP" id="AgentIdP333" class="customTAB" data-agentid="333" data-gameid="333"></select>



<script type="text/javascript">
var agent = document.getElementById('AgentIdP333');
var agentId = agent.getAttribute("data-agentid");
var gameId = agent.getAttribute("data-gameid");
</script>


It’s easier for you to get data with jQuery:

var agentId = $('#AgentIdP333').data('agentid');