EN VI

Javascript - Is there a regex pattern to remove all numbers preceding every "<" in a string?

2024-03-10 10:00:05
Javascript - Is there a regex pattern to remove all numbers preceding every "<" in a string?

While I've utilized simple regex patterns for things like form validation and small string edits, this snippet has proven to be more challenging than my other tasks. My current patterns perform a number of functions including adding line breaks at specific points.

...().replace(/^.\s+|\s+$/gm, '').replace(/>/g, ">\r").replace(/(.{99})/g, "$1\r"));


1<div class="form-group stacked" aria-live="polite" id="test-input">
2<label for="company">
3Business name4<sup>
5*6</sup>
7</label>
8<div class="input-wrapper is-clear">
9<input type="text" class="form-control stacked has-clear" id="company" name="company" required="" 
aria-required="true" value="">
10</input>
11<button class="btn clear-field-button" type="button" tabindex="0" style="display: inline-block;" 
aria-label="Clear Business name input field">
12<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 22.673 22.674">...

Should render as

<div class="form-group stacked" aria-live="polite" id="test-input">
<label for="company">
3Business name<sup>
5*</sup>
</label>
<div class="input-wrapper is-clear">
<input type="text" class="form-control stacked has-clear" id="company" name="company" required="" 
aria-required="true" value="">
</input>
<button class="btn clear-field-button" type="button" tabindex="0" style="display: inline-block;" 
aria-label="Clear Business name input field">
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 22.673 22.674">...

Any guidance and/or solutioning is greatly appreciated. Thank you.

Solution:

Sure, look at character classes. The simplest way is to use "/d+" which is the same as "[0-9]+" followed by the literal "<" as suggested to you by user3783243 in the comment.

const before = document.getElementById("before");
const after = document.getElementById("after");

const page = `1<div class="form-group stacked" aria-live="polite" id="test-input">
2<label for="company">
3Business name4<sup>
5*6</sup>
7</label>
8<div class="input-wrapper is-clear">
9<input type="text" class="form-control stacked has-clear" id="company" name="company" required="" 
aria-required="true" value="">
10</input>
11<button class="btn clear-field-button" type="button" tabindex="0" style="display: inline-block;" 
aria-label="Clear Business name input field">
12<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 22.673 22.674">`;

before.value = page;
after.value = page.replaceAll(/\d+</g, '<');
<h2>Before</h2>
<textarea id="before" rows="15" cols="80"></textarea>
<h2>After</h2>
<textarea id="after" rows="15" cols="80"> </textarea>

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login