Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

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

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:22:40+00:00 2026-05-26T17:22:40+00:00

javascript how to preg match first word and last word from html? for example,

  • 0

javascript how to preg match first word and last word from html?

for example, in the below sentence, I want get words The and underway. Thanks.

<p>The Stack Overflow 2011 community moderator election is underway</p>
//should consider the different html tags.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:22:41+00:00Added an answer on May 26, 2026 at 5:22 pm

    If the element is already in the document, you can get its text content and split it based on " ":

    var text  = "textContent" in document.body ? "textContent" : "innerText",
        el    = document.getElementById("myElement"),
        arr   = el[text].split(" "),
        first = arr.shift(),
        last  = arr.pop();
    
    alert("1st word is '"+first+"', last word is '"+last+"'.");
    

    If it’s not already an element, make it one:

    var arr, first, last,
        text  = "textContent" in document.body ? "textContent" : "innerText",
        html = "<p>The Stack Overflow 2011 community moderator election is underway</p>",
        el   = document.createElement("div");
    
    el.innerHTML = html;
    arr   = el[text].split(" "),
    first = arr.shift(),
    last  = arr.pop();
    
    alert("1st word is '"+first+"', last word is '"+last+"'.");  
    

    Note: this doesn’t take punctuation characters into account – you might want to remove some of them from the string with a simple regex before splitting. Also, if there is only a single word in the text, last will be undefined. If there are no words, 1st will be an empty string, "" and last will still be undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove JavaScript from the HTML. I can't get the regular
i am parsing same HTML i need to get a number from a line
JavaScript allows functions to be treated as objects--if you first define a variable as
I am trying to match some of the Javascript on a Youtube video page.
I need help to create a regex (for JavaScript .match and PHP preg_match) that
I have this code from a javascript /+\uFF0B0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9u/ after some read about php &
I'm trying to replicate CI's humanize() and underscore() function in Javascript. From the CI
I get a CSV data from a SOAP call in php. Unfortunately, the data
I have the following site and I want with regular expressions to get the
I have the following regular expression: RegExp(http://www.amazon.com/([\\w-]+/)?(dp|gp/product)/(\\w+/)?(\\w{10})); Written in javascript. How can I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.