0

I'm creating a read more function.* I want to split my html code at the point where <span id="more-NUMBER"></span> happens. Then I want to move the before and after sections.

The problem I'm running into is writing the regex in a way that can account for the numbers placed in the id. Here's what I'm starting with:

var text = jQuery('p').html().split('<span id="more-/\d+/"></span>');
text[0].appendTo('.red-text');
text[1].appendTo('.blue-text');

Here's a starter Fiddle: https://jsfiddle.net/samwisegrangee/r8c4urht/

*I had one that worked previously when our Wordpress site used a <!--more--> comment to create a read more. You can view that solution here: https://codepen.io/samwisegrangee/pen/aWGJOO

2
  • split('<span id="more-/\d+/"></span>'); That's not a regEx.. it's just a string. Commented Aug 24, 2017 at 19:31
  • Is there a way to make my .split() happen regardless of the number in that string? Commented Aug 24, 2017 at 19:33

2 Answers 2

1

First, a regular expression is denoted by forward slashes (/.../) rather than quotes. Second, in the regex you would need to escape the backslash in </span>.

/<span id="more-\d+"><\/span>/

That's not quite all. Your appends aren't working either, so here's some final corrected code:

var regex = /<span id="more-\d+"><\/span>/;
var text = jQuery('.featured-text p').html().split(regex);
jQuery('.red-text').html(text[0]);
jQuery('.blue-text').html(text[1]);

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

id="more-/\d+/"

Backslash is the escape character. So \" not /". What is the / in more-/ supposed to do?

Working demo.

1 Comment

The "more-" is just the constant part of the id. The number changes but it will always start off with more-.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.